Created
July 12, 2011 15:33
-
-
Save practicingruby/1078218 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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" |
Author
@burke, but the code does have instance variables. That's the thing that's weird about it.
@sandal Oh. I didn't read carefully enough. That is weird.
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
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.