I want puts to return @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.
Why? And what is going wrong here? Is it an Atom thing?
puts prints each argument by calling its to_s method. It also expects the to_s method to return a string. If to_s returns something else it ignores that value and prints the object's class and id instead.
In your example, @value is an integer. To fix your code:
def to_s
@value.to_s
end