Bug #1786: unexpected #inspect behaviour
http://redmine.ruby-lang.org/issues/show/1786

Author: Andy Bogdanov
Status: Open, Priority: Normal
ruby -v: 1.9.1

It's said in the documentation that #inspect, if not overridden should use #to_s. Instead of that, when not overridden, #inspect uses #to_s only until the first instance variable assignment, after that it is overridden by interpreter.

~$ cat > test.rb
class MyClass
  def to_s() "instance of MyClass" end
  def assign
    @var = var
    self
  end
end

~$ irb
irb(main):001:0> load 'test.rb'
=> true
irb(main):002:0> MyClass.new
=> Instance of MyClass
irb(main):003:0> MyClass.new.assign
=> #<MyClass:0x9071620 @val=nil>

Besides that #to_s stays unchanged when it's not overridden and because usually the first instance variable assignment happens in #initialize in most cases #inspect returns something like #<MyClass:0x9071620 @val=nil> and #to_s returns #<MyClass:0x9071620>


----------------------------------------
http://redmine.ruby-lang.org