In Ruby, inspect does not substitute to_s

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

In Ruby, inspect does not substitute to_s
tensorflow does not enter in windows + anaconda.
How to write Ruby to_s in Python
Rock-paper-scissors in Ruby
Rock-paper-scissors in Ruby
Panel does not fit in Box Sizer put in Frame! !! !! !!
Clicking on name attribute in Selenium does not submit
Command when ACPI shutdown does not work in VirtualBox
[Python] Reason why index error does not occur in slice