Feature #1786

unexpected #inspect behaviour

Added by Andy Bogdanov over 6 years ago. Updated over 4 years ago.



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

~$ 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
=> #

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 # and #to_s returns #MyClass:0x9071620

Related issues

Related to Ruby trunk - Bug #4453: Overriding #to_s changes #inspect Closed 03/01/2011


#1 Updated by Roger Pack about 6 years ago

Which versions show this?

#2 Updated by Nobuyoshi Nakada about 6 years ago

  • Status changed from Open to Rejected

to_s isn't inspect.
orverride inspect if you want to change it.

#3 Updated by Yuki Sonoda about 6 years ago

  • Status changed from Rejected to Open
  • Category set to doc
  • Target version set to 1.9.2

I think it is a bug of rdoc documentation.

#4 Updated by Nobuyoshi Nakada about 6 years ago

  • % Done changed from 0 to 100
  • Status changed from Open to Closed

This issue was solved with changeset r25427.
Andy, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


#5 Updated by Yukihiro Matsumoto about 6 years ago


In message "Re: Feature #1786 unexpected #inspect behaviour"
on Wed, 21 Oct 2009 09:29:53 +0900, Nobuyoshi Nakada redmine@ruby-lang.org writes:

|to_s isn't inspect.
|orverride inspect if you want to change it.

But still, the behavior OP reported is weird. Redefining #to_s should
not affect inspect, if they are totally different.

I will work on.



Also available in: Atom PDF