Ruby -Python __str__()方法的等价物?

zy1mlcev  于 12个月前  发布在  Ruby
关注(0)|答案(3)|浏览(114)

在Ruby中,是否有可以在Python类上定义的__str__()方法的等价物?

l5tcr1uw

l5tcr1uw2#

FWIW,inspect可能更像__repr__()而不是__str__()
从图书馆的参考资料中

repr(self)

Called by the repr() built-in function and by string conversions (reverse quotes) to compute the ``official'' string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form "<...some useful description...>" should be returned. The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an ``informal'' string representation of instances of that class is required.

mcdcgff0

mcdcgff03#

在核心类上,它通常是“检查”。
例如:

irb(main):001:0> puts "array is: #{[1,2,3].inspect}"
array is: [1, 2, 3]
=> nil
irb(main):002:0> puts "array is: #{[1,2,3]}"
array is: 123
=> nil
irb(main):003:0>

字符串

相关问题