0

私はすでに算術演算子を処理するために強制を使用しました:

class MyCustomClass

  def coerce( other )
    [MyCustomClass.new(other), self]
  end

end

つまり、次のことができます。

42 + MyCustomClass.new

(モンキーパッチなしで)比較を実行できる同様のメカニズムが存在するかどうかを知りたいFixnum

42 > MyCustomClass.new
4

1 に答える 1

1

演算子を含めComparableて定義するだけです。<=>

class MyCustomClass
  include Comparable

  # ...

  def <=>(other)
    # e.g. compare self to fixnum and return -1, 0, 1
  end
end
于 2013-09-20T09:24:02.543 に答える