class X
def initialize
@name = "Bob"
end
blah blah
end
puts X.new # I want this to print X:Bob
puts [X.new, X.new] # I want this to print [X:Bob, X:Bob]
Run Code Online (Sandbox Code Playgroud)
覆盖to_s您班级的方法:
class X
def initialize
@name = "Bob"
end
def to_s
"X:#{@name}"
end
end
puts X.new # prints X:Bob
puts [X.new, X.new].to_s # prints [X:Bob, X:Bob]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
83 次 |
| 最近记录: |