Numeric defines #== and friends
Reported by Eric Hodel | May 19th, 2008 @ 04:47 PM | in 1.0
Should instead define only spaceship and include Comparable.
Define Fixnum, Float, Bignum#== and friends instead.
Comments and changes to this ticket
-
Ryan Davis June 23rd, 2008 @ 04:10 PM
- → Assigned user changed from to Ryan Davis
-

-
Ryan Davis June 29th, 2008 @ 09:37 PM
I tend to agree...
Also, you need a better username. We're not borg.
-
Arthur Schreiber June 30th, 2008 @ 12:15 AM
@7: No, that's not true. Comparable defines #==. Comparable uses the spaceship operator (that funky #<=> method), as noted above, to define #>, #<, #==, etc.
I tried to fix that, but it was just too much of an headache, heh.
-
Ryan Davis June 30th, 2008 @ 01:33 AM
Comparable depends on spaceship (<=>) to do it's work, which returns [-1, 0, 1]. So to define the other Comparable methods, you need to be able to compare against those fixnums:
le <= 0 (or != 1) lt == -1 eq == 0 gt == 1 ge >= 0 (or != -1)So, technically, I guess you can just have Fixnum#== assuming spaceship never ever takes any shortcuts.
(argh I hate the formatting system in lighthouse--and yes, the compose page is MUCH better by not linking to a quickref. asshats)
-
Arthur Schreiber June 30th, 2008 @ 05:20 AM
Not sure whether we're thinking the same thing. :)
Numeric#<=> should only return 0 if self#equal?(other) is true, nil otherwise. If you now include Comparable into Numeric, everything is fine and works like in MRI.
Numeric should not define #==, #<, #>, #<= or #>= (it currently does), as these get defined by the Comparable module (which will use Numeric#<=> internally).
All (Core)-Numeric subclasses should then define their own (modified) #<=> method, which will get used by the Comparable methods.
Fixnum itself can also redefine all comparison methods (as it does in MRI) for increased performance when doing type-conversions or whatever you want it to do. (Which, as noted by you, could actually be all handled inside Fixnum#<=>).
One thing that I think is weird is that all the Numeric subclasses define their own #<=> and #== method, as they could simply rely on #== being defined by Comparable and using #<=> internally.
I hope this clarifies things up a bit. :)
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
