In Python,
class Foo:
def __repr__(self):
return 'bar'
foo=Foo()
str(foo) # 'bar'
str(foo) gives 'bar' (both in Py2/Py3). So, if __repr__ is defined and __str__ is not defined, __repr__ is called instead.
However, in Ruby,
class Foo
def inspect
'bar'
end
end
foo=Foo.new
foo.to_s # #<Foo:0x0000....>
p foo # bar
puts foo # #<Foo:0x0000....>
foo.to_s does not give 'bar'. So, if you define inspect, you should also define to_s.
For search: in Ruby inspect does not replace to_s
Recommended Posts