Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created July 12, 2011 15:33
Show Gist options
  • Select an option

  • Save practicingruby/1078218 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/1078218 to your computer and use it in GitHub Desktop.
# Is this a bug or just a behavior change? Know a workaround to get the 1.8 behavior in 1.9?
I would even be okay with explicitly having to define inspect as long as
I could get the original inspect behavior from Object back, but would prefer something simpler...
## Ruby 1.9.2
>> class A
>> def initialize(x,y,z)
>> @x, @y, @z = x,y,z
>> end
>> end
=> nil
>> A.new(1,2,3)
=> #<A:0x000001009fbd10 @x=1, @y=2, @z=3>
>> class A
>> def to_s
>> "a"
>> end
>> end
=> nil
>> A.new(1,2,3)
=> a
## Ruby 1.8.7
>> class A
>> def initialize(x,y,z)
>> @x, @y, @z = x,y,z
>> end
>> def to_s
>> "x"
>> end
>> end
=> nil
>> A.new(1,2,3)
=> #<A:0x100341c68 @z=3, @y=2, @x=1>
>> A.new(1,2,3).to_s
=> "x"
@burke

burke commented Jul 12, 2011

Copy link
Copy Markdown

Well, the problem is here... https://github.com/ruby/ruby/blob/trunk/object.c#L417-418

No helpful suggestions, but it does seem like bizarre behaviour.

@practicingruby

Copy link
Copy Markdown
Author

@burke, but the code does have instance variables. That's the thing that's weird about it.

@burke

burke commented Jul 12, 2011

Copy link
Copy Markdown

@sandal Oh. I didn't read carefully enough. That is weird.

@burke

burke commented Jul 12, 2011

Copy link
Copy Markdown

So, looking at https://github.com/ruby/ruby/blob/trunk/object.c#L429 ...

https://github.com/ruby/ruby/blob/trunk/class.c#L1520-1528 checks if the "to_s" method is the to_s defined on Object, and skips right to calling to_s if it isn't. That's not what the documentation says it does :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment