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. # => "