Consider
class Container
def initialize(value = 0)
@value = value
end
def + (other)
return @value + other
end
def - (other)
return @value - other
end
def * (other)
return @value * other
end
def / (other)
return @value / other
end
def get
return @value
end
end
I would like to use +=
to increase the value in the container, like this:
c = Container.new(100)
c += 100
print c.get # Expecting 200
The above won't work, as 100
will overwrite c
.
I know I could use something like an attr_accessor
to generate a getter and setter for the value, but I'm curious if I could do this in a prettier way such as using +=
.