On Tue, 19 Nov 2002 21:13:11 +0000, Lyle Johnson wrote:

> Tim Hunter wrote:
> 
>> I need to define <=> in a class. What is the appropriate thing to do when
>> the "other" argument is not an object in the class? For example, comparing
>> myObject <=> nil, which actually happens in the debugger. Should I just
>> send it to the superclass?
> 
> If you can somehow coerce the other argument into the receiver's class, 
> that would work. Otherwise, I think most or all of the built-in classes 
> just raise a TypeError exception.

Thanks to everybody for their help! Actually, I had mistakenly asked two 
questions in one. The first question should've been "what do I do in my
== method when the other argument isn't an object in the same class?" The 
answer is obviously "return false".

The second question should've been "what about the <=> method?" and the
answer is "raise TypeError".

Here's more-or-less what I ended up with:

    def ==(other)
        return false unless other.kind_of? self.class
        return (self <=> other) == 0 ? true : false
    end

    def <=>(other)
        unless other.kind_of? MyClass
           raise TypeError, "#{other.class} can't be coerced into MyClass"
        end
	# do the comparison & return the result
    end

Any suggestions?