如何覆盖类的常见运算符,例如+
, -
, *
, /
, <
, ==
,>
等?
问问题
300 次
1 回答
6
根据它们被覆盖的方式,它们不一定是二进制的。
class Foo
def +; :plus end
def -; :minus end
def *; :asterisk end
def /; :slash end
def <; :lt end
def ==; :eq end
def >; :gt end
end
Foo.new.+ # => :plus
Foo.new.- # => :minus
Foo.new.* # => :asterisk
Foo.new./ # => :slash
Foo.new.< # => :lt
Foo.new.== # => :eq
Foo.new.> # => :gt
于 2013-03-27T06:34:36.450 回答