is there a way to use the spaceship method and the magic “method_missing” in one class? The below example gives me undefined method '<' whenever I try foo1 < foo2 with a definiton like:
class Foo
def initialize(params)
@parent= params[:parent]
end
def <=>(o)
...
end
def method_missing(sym, *args, &block)
@parent.send sym, *args, &block
end
end
Any help appreciated 🙂
short answer:
You’re missing this line of code inside
Foo:long answer:
You don’t get the
<,>,<=, etc. methods just by redefining the spaceship operator.You get those methods by including
Comparable. Those methods then use the spaceship operator to provide a valid response.It’s more or less what happens with
Enumerable:you include the module, implement the
eachmethod, and then get all the other methods (map,select, etc) for “free”.