[英]Does Atom break ruby to_s override?
I want puts to return @value.我想让 put 返回 @value。 The code is below:
代码如下:
class Tile
def initialize(value, given=false)
@value = value
@given = given
end
def newValue(value)
@value = value if @given == false
end
def to_s
@value
end
end
t = Tile.new(5)
t.newValue(6)
debugger
puts t
This results in the printing of just the object id, no variables.这导致仅打印 object id,没有变量。
Why?为什么? And what is going wrong here?
这里出了什么问题? Is it an Atom thing?
它是 Atom 的东西吗?
puts
prints each argument by calling its to_s
method. puts
通过调用其to_s
方法打印每个参数。 It also expects the to_s
method to return a string .它还期望
to_s
方法返回一个字符串。 If to_s
returns something else it ignores that value and prints the object's class and id instead.如果
to_s
返回其他内容,它会忽略该值并打印对象的 class 和 id。
In your example, @value
is an integer. To fix your code:在您的示例中,
@value
是 integer。要修复您的代码:
def to_s
@value.to_s
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.