要查看谁在扮演self
in的角色nested methods
,我尝试了以下代码:
def test
p "#{self}"
def show
p "#{self}"
end
end
# => nil
作为效果,我得到了以下两个对象:
Object.new.test
"#<Object:0x00000002212d78>"
# => nil
Object.new.test.show
"#<Object:0x00000002205330>" #<~~~ this is self understood
"" #<~~~ how this one came?
# => ""
但是从编码的数字来看,我无法理解这些对象属于哪个类。我尝试了下面的代码并获得了各自的class
名称。
Object.new.test.class
"#<Object:0x000000021ff3b8>"
# => NilClass
Object.new.test.show.class
"#<Object:0x000000020660b0>"
""
# => String
那么任何人都可以帮助我理解上述代码如何产生这些class
名称的概念吗?
编辑
在这里,我试图以更具体的方式提出我的问题:
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
为什么p "Second level # => #{self}"
声明self
有价值""
?