Ruby Overriding Operator
I have a class like this:
class MyObject
cattr_accessor :value_ # Note the `cattr_accessor` as opposed t开发者_如何学编程o `attr_accessor`.
def +(right_)
self.value_ + right_.value_
end
end
I want to be able to do something like this:
x = MyObject.new
y = MyObject.new
x.value_ = 1
y.value_ = 2
puts x + y
It's not working though.
My method works with attr_accessor.
精彩评论