On Jul 16, 2:47 am, "John Lam (CLR)" <jf... / microsoft.com> wrote:

> Florian has an interesting test that demonstrates this behavior:
>
>   it "returns nil if its argument does not respond to <=>" do
>     obj = Object.new
>     def obj.to_str() "" end
>
>     ("abc" <=> obj).should == nil
>   end

This is quite odd actually. MRI just does a respond_to?() on to_str
here. If that returns true it actually calls <=>. This is the same way
it handles == for all classes. (See
http://rubyforge.org/tracker/index.php?func=detail&aid=11585&group_id=426&atid=1698
for a request to change the == semantics.)

The thing to note here is that String#<=> doesn't use the same
semantics as Array#<=>:

obj = Object.new
def obj.to_ary() [] end
def obj.to_str() "" end

"foo" <=> obj # => nil
[5] <=> obj # => 1

I think this should be changed in MRI. Either all <=> methods should
use to_* as an indicator without actually calling it or they should
all call it. I'd prefer the last, because the indicator behaviour is
somewhat unusual. (I'd like to see it changed for == as well...)

Is anyone from the MRI core team listening?