This is a multi-part message in MIME format.

------extPart_000_0001_01C20044.520439E0
Content-Type: text/plain;
	charsets-ascii"
Content-Transfer-Encoding: 7bit

"Yukihiro Matsumoto" <matz / ruby-lang.org> wrote in
...
> |  module Comparable
> |    def <(other)
> |      result  elf <other
> |      return result.nil? ? false : result
> |    end
> |  end
> |
> |Thus <could have five possible results:
> |    -1       less than
> |     0       equal
> |     1       greater than
> |    nil      none of the above
> |  exception  an exceptional condition (error) occurred
>
> Hmm, I like this one.  Let me consider.

Yes please;-)  The included patch for ``compare.c'' implements
this. The cosmetic object.c patch is for consistency only and
not necessary  (Module#<is not a partial order anyway).


/Christoph


------extPart_000_0001_01C20044.520439E0
Content-Type: application/octet-stream;
	name
ompar.diff" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename
ompar.diff" --- compar.c.1.11 Mon May 20 15:48:27 2002 +++ compar.c Mon May 20 18:28:08 2002 @@ -17,32 +17,13 @@ tatic ID cmp; 0Atatic VALUE -cmp_eq(a) - VALUE *a; -{ - VALUE c = rb_funcall(a[0], cmp, 1, a[1]); - int t = NUM2INT(c); - - if (t == 0) return Qtrue; - return Qfalse; -} - -static VALUE -cmp_failed() -{ - return Qfalse; -} - -static VALUE mp_equal(x, y) ALUE x, y; - VALUE a[2]; - - if (x == y) return Qtrue; + VALUE c = rb_funcall(x, cmp, 1, y); 0A- a[0] = x; a[1] = y; - return rb_rescue(cmp_eq, (VALUE)a, cmp_failed, 0); + if (!NIL_P((c)) && NUM2LONG(c) == 0) return Qtrue; + return Qfalse; 0Atatic VALUE @@ -50,9 +31,8 @@ ALUE x, y; ALUE c = rb_funcall(x, cmp, 1, y); - int t = NUM2INT(c); 0A- if (t > 0) return Qtrue; + if (!NIL_P(c) && NUM2LONG(c) > 0) return Qtrue; eturn Qfalse; 0A@@ -61,9 +41,8 @@ ALUE x, y; ALUE c = rb_funcall(x, cmp, 1, y); - int t = NUM2INT(c); 0A- if (t >= 0) return Qtrue; + if (!NIL_P(c) && NUM2LONG(c) >= 0) return Qtrue; eturn Qfalse; 0A@@ -72,9 +51,8 @@ ALUE x, y; ALUE c = rb_funcall(x, cmp, 1, y); - int t = NUM2INT(c); 0A- if (t < 0) return Qtrue; + if (!NIL_P(c) && NUM2LONG(c) < 0) return Qtrue; eturn Qfalse; 0A@@ -83,9 +61,8 @@ ALUE x, y; ALUE c = rb_funcall(x, cmp, 1, y); - int t = NUM2INT(c); 0A- if (t <= 0) return Qtrue; + if (!NIL_P(c) && NUM2LONG(c) <= 0) return Qtrue; eturn Qfalse; 0A@@ -94,14 +71,22 @@ ALUE x, min, max; ALUE c = rb_funcall(x, cmp, 1, min); - long t = NUM2LONG(c); - if (t < 0) return Qfalse; + VALUE d = rb_funcall(x, cmp, 1, max); 0A- c = rb_funcall(x, cmp, 1, max); - t = NUM2LONG(c); - if (t > 0) return Qfalse; + if (NIL_P(c)) { + if (NIL_P(d) || NUM2LONG(d)) return Qfalse; + } else { + long s = NUM2LONG(c); + if (NIL_P(d)) { + return Qfalse; + } else { + long t = NUM2LONG(d); + if (s <= 0 || t >= 0) return Qfalse; + } + } eturn Qtrue; -} +}; + 0Aoid nit_Comparable() -----extPart_000_0001_01C20044.520439E0 Content-Type: application/octet-stream; namebject.diff" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filenamebject.diff" --- object.c.1.78 Mon May 20 15:47:10 2002 +++ object.c Mon May 20 15:53:56 2002 @@ -604,7 +604,6 @@ return INT2FIX(1); arg = RCLASS(arg)->super; - rb_raise(rb_eArgError, "non related class/module"); eturn Qnil; /* not reached */ 0A@@ -872,7 +871,7 @@ NIL_P(val) ? "nil" : val == Qtrue ? "true" : val == Qfalse ? "false" : - rb_class2name(CLASS_OF(val)), + rb_class2name(CLASS_OF(val)), tname); } else { @@ -1232,7 +1231,7 @@ b_define_method(rb_mKernel, "nil?", rb_false, 0); b_define_method(rb_mKernel, "==", rb_obj_equal, 1); b_define_method(rb_mKernel, "equal?", rb_obj_equal, 1); - rb_define_method(rb_mKernel, "===", rb_obj_equal, 1); + rb_define_method(rb_mKernel, "===", rb_obj_equal, 1); b_define_method(rb_mKernel, "=~", rb_false, 1); 0Ab_define_method(rb_mKernel, "eql?", rb_obj_equal, 1); -----extPart_000_0001_01C20044.520439E0 Content-Type: application/octet-stream; name
omp_test.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename
omp_test.rb" class A def initialize(a) @a = a end include Comparable attr_reader :a def <=> (other) @a <=> other.a rescue nil end end x = A.new -10 y = A.new 4.5 z = A.new 'z' p ([x < y,x <=y, x == y, x >= y, x > y] == \ [true, true, false,false,false ]) p ([y < x,y <=x, y == x, y >= x, y > x] == \ [false,false,false, true, true]) p ([-10.0,1,4.5,'a'].collect {|n| (A.new n).between? (x,y) } == \ [false,true,false,false]) p ([-10.0,1,4.5,'a'].collect {|n| (A.new n).between? (y,x) } == \ false,false,false,false]) x = A.new 'x' p ([x < y,x <=y, x == y, x >= y, x > y] == \ [false, false, false,false,false ]) def x.<=>(other) '42' unless other.a == 'z' end 0A p [ proc { x < y }, proc { x <=y }, proc { x == y }, proc { x >= y }, proc { x > y }, proc { x.between?(z,y) }, proc { x.between?(y,z) } ].collect {|l| begin l.call rescue => mes mes.type end } == Array.new(7) {TypeError} -----extPart_000_0001_01C20044.520439E0--