To see who is playing the role of `self` in `nested methods`, I tried
the below code:
def test
p "#{self}"
def show
p "#{self}"
end
end
# => nil
As an effect, I got the two objects below:
Object.new.test
"#<Object:0x00000002212d78>"
# => nil
Object.new.test.show
"#<Object:0x00000002205330>" #<~~~ this is self understood
"" #<~~~ how this one came?
# => ""
But from the encoded numbers, I couldn't understand which class those
objects belong to. And I tried the code below and got the respective
`class` names.
Object.new.test.class
"#<Object:0x000000021ff3b8>"
# => NilClass
Object.new.test.show.class
"#<Object:0x000000020660b0>"
""
# => String
So can anyone help me to understand the concept of how the above code
produced those `class` names?
on 2013-02-26 19:19
on 2013-02-26 20:08
Doing more play with it I found the below code:
def test
p "First level # => #{self}"
def show
p "Second level # => #{self}"
end
end
# => nil
Object.new.test.show
"First level # => #<Object:0x000000014a77b0>"
"Second level # => "
# => "Second level # => "
Object.new.test.show.class
"First level # => #<Object:0x0000000130ef70>"
"Second level # => "
# => String
Why the `p "Second level # => #{self}"` statement self has `""` value?
on 2013-02-26 21:24
How about reading a Ruby book or two, or working through codecademy.com/tracks/ruby BEFORE asking any more questions. You haven't the slightest clue about the Ruby language at ALL. Before you waste this list's time further, do some studying. You are not owed help from this list, JUST because you joined. You have to put in EFFORT in your learning for the list to aid you, which people have tried to do even though you show no effort on your part. Work on the basics by working through the URLs, and books you were assigned before asking for further assistance.
on 2013-02-26 21:48
Here is the answer:
Would feel great if anyone helped me here to understand the topic. But
anyway not encouraging other's talk myself produced the answer for
future comers.
We all know that - ***`self inside a method is always the object on
which the method was called`***. So let's try and examine the
truthfulness of that.
Let's see who is default `self` from the below code first of all:
m=self
# => main
m.class
# => Object
Okay, the default `self` is the object of `Object` class.
Just written the below code in more simplified way, from the description
mentioned code,to highlight on the concept.
def test
p self.class
def show
p self.class
end
end
# => nil
Keeping in mind `self inside a method is always the object on which the
method was called` called only `test` as below.
test
Object
# => nil
Yes, `Object` has been returned,on which the `test` has been called,that
means the above statement is true.
test.show
Object
NilClass
# => NilClass
Calling to `test` also returns `nil` due to the block `def show;p
self.class;end`.Now `nil` is an object of `NilClass`. Thus `show` method
has been called on `NilClass` object. As result `self` is `NilClass`.
Again the above statement holds.
With the above concept trying to reach to the actual goal with tiny
steps:
def test
p "1. # => #{self}"
def show
p "2. # => #{self}"
end
end
# => nil
test
"1. # => main" #<~~ main is an object of class Object,on which test
was called from IRB.
# => nil
test.show
"1. # => main"
"2. # => " #<~~ nil("" means nil.to_s) is an object of
Nilclass,on which show was called from IRB.
# => "2. # => "
on 2013-02-26 21:53
D. Deryl Downey wrote in post #1099203: > How about reading a Ruby book or two, or working through > codecademy.com/tracks/ruby BEFORE asking any more questions. You haven't > the slightest clue about the Ruby language at ALL. > Good advice, but you should also know that,can't read 3-4 books at a time. Only one I can. And There sometime I got stuck and with hope here I posted,so that to whom the post of mine might seem very easy,can help me out. Rather 5-6 people like you creating noises which effects other interested users also to come forward. Thanks.
on 2013-02-26 22:41
Am 26.02.2013 21:53, schrieb Love U Ruby: > D. Deryl Downey wrote in post #1099203: >> How about reading a Ruby book or two, or working through >> codecademy.com/tracks/ruby BEFORE asking any more questions. You haven't >> the slightest clue about the Ruby language at ALL. >> > > Good advice, but you should also know that,can't read 3-4 books at a > time. Only one I can. Which one are you reading right now?
on 2013-02-26 22:44
unknown wrote in post #1099221: > Am 26.02.2013 21:53, schrieb Love U Ruby: >> D. Deryl Downey wrote in post #1099203: >>> How about reading a Ruby book or two, or working through >>> codecademy.com/tracks/ruby BEFORE asking any more questions. You haven't >>> the slightest clue about the Ruby language at ALL. >>> >> >> Good advice, but you should also know that,can't read 3-4 books at a >> time. Only one I can. > > Which one are you reading right now? Russ Olsen "Eloquent Ruby".
on 2013-02-26 23:15
Am 26.02.2013 22:44, schrieb Love U Ruby: >> >> Which one are you reading right now? > > Russ Olsen "Eloquent Ruby". It's a great book but not meant for beginners.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.