【发布时间】:2010-04-12 20:19:23
【问题描述】:
python的__repr__函数很花哨,它是在自动使用print OBJECT时调用的。
它有红宝石等价物吗?我以为是 to_s,但是,我有 p OBJECT 似乎没有调用 to_s 方法。
添加
我搞错了,p OBJECT 似乎调用 to_s 方法如下。 我从另一个问题的答案中得到了一些提示。 - Ruby's to_s method question (from Axe book 2nd edition)
# Sample code from Programing Ruby, page 24
class Song
def to_s
"Song"
end
end
class Songson < Song
def to_s
super + "<Songson>"
end
end
song = Songson.new()
p song
【问题讨论】: