From: Warren Brown Date: 2003-02-22T01:23:21+09:00 Subject: Re: Inconsistent behavior in Array methods Bulat, > WB> though Array#sort uses the object's <=> method. > > are you sure? :) > > class Fixnum > alias q <=> > def <=>x > -q(x) > end > end > > p [1,2,3,4].sort #=>[1, 2, 3, 4] > p [1,2,3,4].sort {|a,b| a<=>b} #=>[4, 3, 2, 1] Quite sure: irb(main):001:0> RUBY_VERSION => "1.6.8" irb(main):002:0> class A; end => nil irb(main):003:0> [A.new,A.new].sort NameError: undefined method `<=>' for # from (irb):3:in `sort' from (irb):3 There are special cases for Fixnum and String in Array#sort: if (FIXNUM_P(a) && FIXNUM_P(b)) { if (a > b) return 1; if (a < b) return -1; return 0; } if (TYPE(a) == T_STRING && TYPE(b) == T_STRING) { return rb_str_cmp(a, b); } retval = rb_funcall(a, cmp, 1, b); return rb_cmpint(retval); - Warren Brown