开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜