From: Christoph Date: 2002-05-21T04:22:20+09:00 Subject: RE: Exceptions and <=> This is a multi-part message in MIME format. ------=_NextPart_000_0001_01C20044.520439E0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit "Yukihiro Matsumoto" wrote in ... > | module Comparable > | def <(other) > | result = self <=> 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 ------=_NextPart_000_0001_01C20044.520439E0 Content-Type: application/octet-stream; name="compar.diff" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="compar.diff" --- compar.c.1.11 Mon May 20 15:48:27 2002=0A= +++ compar.c Mon May 20 18:28:08 2002=0A= @@ -17,32 +17,13 @@=0A= static ID cmp;=0A= =0A= static VALUE=0A= -cmp_eq(a)=0A= - VALUE *a;=0A= -{=0A= - VALUE c =3D rb_funcall(a[0], cmp, 1, a[1]);=0A= - int t =3D NUM2INT(c);=0A= -=0A= - if (t =3D=3D 0) return Qtrue;=0A= - return Qfalse;=0A= -}=0A= -=0A= -static VALUE=0A= -cmp_failed()=0A= -{=0A= - return Qfalse;=0A= -}=0A= -=0A= -static VALUE=0A= cmp_equal(x, y)=0A= VALUE x, y;=0A= {=0A= - VALUE a[2];=0A= -=0A= - if (x =3D=3D y) return Qtrue;=0A= + VALUE c =3D rb_funcall(x, cmp, 1, y);=0A= =0A= - a[0] =3D x; a[1] =3D y;=0A= - return rb_rescue(cmp_eq, (VALUE)a, cmp_failed, 0);=0A= + if (!NIL_P((c)) && NUM2LONG(c) =3D=3D 0) return Qtrue;=0A= + return Qfalse;=0A= }=0A= =0A= static VALUE=0A= @@ -50,9 +31,8 @@=0A= VALUE x, y;=0A= {=0A= VALUE c =3D rb_funcall(x, cmp, 1, y);=0A= - int t =3D NUM2INT(c);=0A= =0A= - if (t > 0) return Qtrue;=0A= + if (!NIL_P(c) && NUM2LONG(c) > 0) return Qtrue;=0A= return Qfalse;=0A= }=0A= =0A= @@ -61,9 +41,8 @@=0A= VALUE x, y;=0A= {=0A= VALUE c =3D rb_funcall(x, cmp, 1, y);=0A= - int t =3D NUM2INT(c);=0A= =0A= - if (t >=3D 0) return Qtrue;=0A= + if (!NIL_P(c) && NUM2LONG(c) >=3D 0) return Qtrue;=0A= return Qfalse;=0A= }=0A= =0A= @@ -72,9 +51,8 @@=0A= VALUE x, y;=0A= {=0A= VALUE c =3D rb_funcall(x, cmp, 1, y);=0A= - int t =3D NUM2INT(c);=0A= =0A= - if (t < 0) return Qtrue;=0A= + if (!NIL_P(c) && NUM2LONG(c) < 0) return Qtrue;=0A= return Qfalse;=0A= }=0A= =0A= @@ -83,9 +61,8 @@=0A= VALUE x, y;=0A= {=0A= VALUE c =3D rb_funcall(x, cmp, 1, y);=0A= - int t =3D NUM2INT(c);=0A= =0A= - if (t <=3D 0) return Qtrue;=0A= + if (!NIL_P(c) && NUM2LONG(c) <=3D 0) return Qtrue;=0A= return Qfalse;=0A= }=0A= =0A= @@ -94,14 +71,22 @@=0A= VALUE x, min, max;=0A= {=0A= VALUE c =3D rb_funcall(x, cmp, 1, min);=0A= - long t =3D NUM2LONG(c);=0A= - if (t < 0) return Qfalse;=0A= + VALUE d =3D rb_funcall(x, cmp, 1, max);=0A= =0A= - c =3D rb_funcall(x, cmp, 1, max);=0A= - t =3D NUM2LONG(c);=0A= - if (t > 0) return Qfalse;=0A= + if (NIL_P(c)) {=0A= + if (NIL_P(d) || NUM2LONG(d)) return Qfalse;=0A= + } else {=0A= + long s =3D NUM2LONG(c);=0A= + if (NIL_P(d)) {=0A= + return Qfalse;=0A= + } else {=0A= + long t =3D NUM2LONG(d);=0A= + if (s <=3D 0 || t >=3D 0) return Qfalse;=0A= + }=0A= + }=0A= return Qtrue;=0A= -}=0A= +};=0A= +=0A= =0A= void=0A= Init_Comparable()=0A= ------=_NextPart_000_0001_01C20044.520439E0 Content-Type: application/octet-stream; name="object.diff" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="object.diff" --- object.c.1.78 Mon May 20 15:47:10 2002=0A= +++ object.c Mon May 20 15:53:56 2002=0A= @@ -604,7 +604,6 @@=0A= return INT2FIX(1);=0A= arg =3D RCLASS(arg)->super;=0A= }=0A= - rb_raise(rb_eArgError, "non related class/module");=0A= return Qnil; /* not reached */=0A= }=0A= =0A= @@ -872,7 +871,7 @@=0A= NIL_P(val) ? "nil" :=0A= val =3D=3D Qtrue ? "true" :=0A= val =3D=3D Qfalse ? "false" :=0A= - rb_class2name(CLASS_OF(val)), =0A= + rb_class2name(CLASS_OF(val)),=0A= tname);=0A= }=0A= else {=0A= @@ -1232,7 +1231,7 @@=0A= rb_define_method(rb_mKernel, "nil?", rb_false, 0);=0A= rb_define_method(rb_mKernel, "=3D=3D", rb_obj_equal, 1);=0A= rb_define_method(rb_mKernel, "equal?", rb_obj_equal, 1);=0A= - rb_define_method(rb_mKernel, "=3D=3D=3D", rb_obj_equal, 1); =0A= + rb_define_method(rb_mKernel, "=3D=3D=3D", rb_obj_equal, 1);=0A= rb_define_method(rb_mKernel, "=3D~", rb_false, 1);=0A= =0A= rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);=0A= ------=_NextPart_000_0001_01C20044.520439E0 Content-Type: application/octet-stream; name="comp_test.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="comp_test.rb" class A=0A= def initialize(a)=0A= @a =3D a=0A= end=0A= include Comparable=0A= attr_reader :a=0A= def <=3D> (other)=0A= @a <=3D> other.a rescue nil =0A= end=0A= end=0A= =0A= x =3D A.new -10=0A= y =3D A.new 4.5=0A= z =3D A.new 'z'=0A= =0A= p ([x < y,x <=3Dy, x =3D=3D y, x >=3D y, x > y] =3D=3D \=0A= [true, true, false,false,false ])=0A= =0A= p ([y < x,y <=3Dx, y =3D=3D x, y >=3D x, y > x] =3D=3D \=0A= [false,false,false, true, true])=0A= =0A= p ([-10.0,1,4.5,'a'].collect {|n| (A.new n).between? (x,y) } =3D=3D \=0A= [false,true,false,false])=0A= =0A= p ([-10.0,1,4.5,'a'].collect {|n| (A.new n).between? (y,x) } =3D=3D \=0A= [false,false,false,false])=0A= =0A= =0A= x =3D A.new 'x'=0A= =0A= p ([x < y,x <=3Dy, x =3D=3D y, x >=3D y, x > y] =3D=3D \=0A= [false, false, false,false,false ])=0A= =0A= =0A= def x.<=3D>(other) =0A= '42' unless other.a =3D=3D 'z' =0A= end=0A= =0A= =0A= p [ proc { x < y },=0A= proc { x <=3Dy },=0A= proc { x =3D=3D y }, =0A= proc { x >=3D y },=0A= proc { x > y },=0A= proc { x.between?(z,y) },=0A= proc { x.between?(y,z) }=0A= ].collect {|l| =0A= begin =0A= l.call=0A= rescue =3D> mes=0A= mes.type =0A= end=0A= } =3D=3D Array.new(7) {TypeError} =0A= ------=_NextPart_000_0001_01C20044.520439E0--