A
Andy Bogdanov
The documentation says "if not overridden, #inspect method uses #to_s to
generate the string", but it turns out that it's behavior is more
complicated: if it's not overridden in some place, it uses #to_s until
the first instance variable assignment, after that it uses Object#to_s
-----------------------------
~$ cat > test.rb
class Test
def assign
@var = nil
self
end
def to_s
"instance of Test"
end
end
~$ irb
irb(main):001:0> load 'test.rb'
=> true
irb(main):002:0> Test.new
=> instance of Test
irb(main):003:0> Test.new.assign
=> #<Test:0x9371320 @var=nil>
irb(main):004:0> RUBY_VERSION
=> "1.9.1"
generate the string", but it turns out that it's behavior is more
complicated: if it's not overridden in some place, it uses #to_s until
the first instance variable assignment, after that it uses Object#to_s
-----------------------------
~$ cat > test.rb
class Test
def assign
@var = nil
self
end
def to_s
"instance of Test"
end
end
~$ irb
irb(main):001:0> load 'test.rb'
=> true
irb(main):002:0> Test.new
=> instance of Test
irb(main):003:0> Test.new.assign
=> #<Test:0x9371320 @var=nil>
irb(main):004:0> RUBY_VERSION
=> "1.9.1"