作者:上帝的宝贝1 | 来源:互联网 | 2023-10-12 09:22
The__repr__functionofpythonisfancyasitiscalledwhenprintOBJECTisusedautomatically.py
The __repr__
function of python is fancy as it is called when print OBJECT is used automatically.
python的__repr__函数很自然,因为在自动使用print OBJECT时会调用它。
Is there a ruby equivalence for it? I thought it was to_s, but, I had p OBJECT doesn't seem to call the to_s method.
它有红宝石等价吗?我以为是to_s,但是,我有p OBJECT似乎没有调用to_s方法。
Added
I got something wrong, p OBJECT seems to call to_s method as follows. I got some hints from my the answers to my other question. - Ruby's to_s method question (from Axe book 2nd edition)
我弄错了,p OBJECT似乎调用to_s方法如下。我从我对其他问题的答案中得到了一些提示。 - Ruby的to_s方法问题(来自Axe第2版)
# Sample code from Programing Ruby, page 24
class Song
def to_s
"Song"
end
end
class Songson "
end
end
sOng= Songson.new()
p song
2 个解决方案