【发布时间】:2013-02-27 14:43:33
【问题描述】:
当通过对象调用该方法时,我正在使用该方法返回的最后一个表达式。
使用以下代码的概念。
class Simple
def foo
2 + 2
end
end
#=> nil
simple = Simple.new
#=> #<Simple:0x11eb390>
simple.foo
#=> 4
但是为什么下面的代码返回这样的编码值而不是15和20呢?
class Fixnum
def to_s
self + 5
end
end
#=> nil
puts 10
#<Fixnum:0x000015>
#=> nil
puts 15
#<Fixnum:0x00001f>
#=> nil
谁能帮我理解这个概念?
编辑:
class Fixnum
def to_s
self + 5
end
end
#=> nil
10.to_s
#=> #<Fixnum:0x000029>
同样的结果。
【问题讨论】:
标签: ruby