From: Robert Klemme Date: 2005-11-10T18:32:49+09:00 Subject: Re: Numeric <=> daz wrote: > Robert Klemme wrote: >> Christophe Grandsire wrote: >>> Selon ChrisH: >>> >>>> Thanks Eric. I just wonder why Numeric doesn't implement it >>>> properly? >>> >>> Probably because it can't. I'm not completely up-to-date with the >>> Numeric inheritance chain, but I guess for instance Complex numbers >>> also inherit from Numeric. Being Numeric doesn't necessarily mean >>> that there is a meaningful order among the objects. >> >> Although I agree with your analysis that Numeric can't properly >> implement <=> the question remains why <=> is actually implemented >> in Numeric. ATM I cannot see what necessitates it. Including >> Comparable is not a reason IMHO because that will break either way >> (i.e. with missing <=> and with incomplete implemented <=>). >> >>> Then again, I'm out on a limb here, and may be completely wrong. >> >> Not completely but I have the feeling we're still not there. Someone >> probably needs to take the time and have a look at the sources... >> > > #------------- > class Roo < Numeric; end > > a = Roo.new > b = Roo.new > c = a > > p a <=> b #-> nil > p a <=> c #-> 0 > #------------- > > Well, that's helpful! > > Ruby (like me) hasn't a clue what a 'Roo' is, > but it's able to tell that a == c which might > be enough to keep a routine running. Definitely not: >> ar=(0..2).map { Roo.new } => [#, #, #] >> ar.sort ArgumentError: comparison of Roo with Roo failed from (irb):14:in `sort' from (irb):14 from :0 >> Roo.new < Roo.new ArgumentError: comparison of Roo with Roo failed from (irb):21:in `<' from (irb):21 from :0 This is what happens in the "standard cases". >> class Bar;include Comparable; end => Bar >> ar=(0..2).map { Bar.new } => [#, #, #] >> ar.sort NoMethodError: undefined method `<=>' for # from (irb):20:in `sort' from (irb):20 from :0 >> Foo = Class.new => Foo >> ar=(0..2).map { Foo.new } => [#, #, #] >> ar.sort NoMethodError: undefined method `<=>' for # from (irb):17:in `sort' from (irb):17 from :0 > > Wed Nov 20 01:52:21 2002 Yukihiro Matsumoto > > * numeric.c (num_cmp): added to satisfy Comparable assumption. Hm... Doesn't sound as if the assumption was satisfied... > http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/numeric.c.diff?r1=1.59;r2=1.60;f=h Kind regards robert