オブジェクトを介して呼び出されているときに、メソッドによって返される最後の式で遊んでいました。
以下の概念でコードが機能します。
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>
再び同じ結果。