Neither to_s nor to_str appear to get called when an object is referenced inside a double-quoted string interpolation. For example:
# UPDATE: This example actually works as expected. See update below.
class Foo
def to_s
'foo'
end
def to_str
to_s
end
end
"#{Foo.new}" # result: "#<Foo:0x007fb115c512a0>"
I don’t suppose there’s something I can do to make the return value "foo"?
UPDATE
Apologies, but this code actually works. Mistake in another piece of code.
With what version of Ruby are you seeing these results? This works correctly for me with Ruby 1.9.2 and 1.8.6: