Project

General

Profile

Actions

Feature #1786

closed

unexpected #inspect behaviour

Added by evilbrain (Andy Bogdanov) over 15 years ago. Updated over 13 years ago.

Status:
Closed
Assignee:
-
Target version:
[ruby-core:24425]

Description

=begin
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
=end


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #4453: Overriding #to_s changes #inspectClosedmame (Yusuke Endoh)03/01/2011Actions
Actions #1

Updated by rogerdpack (Roger Pack) about 15 years ago

=begin
Which versions show this?
-r
=end

Actions #2

Updated by nobu (Nobuyoshi Nakada) about 15 years ago

  • Status changed from Open to Rejected

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

Actions #3

Updated by yugui (Yuki Sonoda) about 15 years ago

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

=begin
I think it is a bug of rdoc documentation.
=end

Actions #4

Updated by nobu (Nobuyoshi Nakada) about 15 years ago

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

=begin
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.

=end

Actions #5

Updated by matz (Yukihiro Matsumoto) about 15 years ago

=begin
Hi,

In message "Re: [ruby-core:26193] Feature #1786 unexpected #inspect behaviour"
on Wed, 21 Oct 2009 09:29:53 +0900, Nobuyoshi Nakada 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.

						matz.

=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0