私はすでに算術演算子を処理するために強制を使用しました:
class MyCustomClass
def coerce( other )
[MyCustomClass.new(other), self]
end
end
つまり、次のことができます。
42 + MyCustomClass.new
(モンキーパッチなしで)比較を実行できる同様のメカニズムが存在するかどうかを知りたいFixnum
:
42 > MyCustomClass.new
私はすでに算術演算子を処理するために強制を使用しました:
class MyCustomClass
def coerce( other )
[MyCustomClass.new(other), self]
end
end
つまり、次のことができます。
42 + MyCustomClass.new
(モンキーパッチなしで)比較を実行できる同様のメカニズムが存在するかどうかを知りたいFixnum
:
42 > MyCustomClass.new
演算子を含めComparable
て定義するだけです。<=>
class MyCustomClass
include Comparable
# ...
def <=>(other)
# e.g. compare self to fixnum and return -1, 0, 1
end
end