M
Manuel Kasten
Hello,
I have written a class, which includes the Comparable Mixin. I
implemented the <=> operator in a way, which maps it to the <=> operator
in integer if applicable and returning nil otherwise. (This is how I
observed it in the Integer class [irb line 006]). I now expected the ==
operator in my class to behave exactly like the == in Integer. This
works fine, while it is applicable (if the Modnums have the same base).
But if I return nil (just like Integer <=> String) <= is fine [line
009], but == behaves differently from what I expected. Why doesn't [line
11] return false like [line 10] does? I thought == is supposed to return
true or false, nothing else?
Thanks for your help,
Manuel
----------------------
irb(main):001:0> require "modnum.rb"
=> true
irb(main):002:0> a = 5
=> 5
irb(main):003:0> b = "Hallo"
=> "Hallo"
irb(main):004:0> c = Modnum.new(3,7)
=> #<Modnum:0x101ae1c8 @base=7, @value=3>
irb(main):005:0> d = Modnum.new(3,9)
=> #<Modnum:0x101a6c20 @base=9, @value=3>
irb(main):006:0> a <=> b
=> nil
irb(main):007:0> c <=> d
=> nil
irb(main):008:0> a <= b
ArgumentError: comparison of Fixnum with String failed
from (irb):8:in `<='
from (irb):8
irb(main):009:0> c <= d
ArgumentError: comparison of Modnum with Modnum failed
from (irb):9:in `<='
from (irb):9
irb(main):010:0> a == b
=> false
irb(main):011:0> c == d
=> nil
I have written a class, which includes the Comparable Mixin. I
implemented the <=> operator in a way, which maps it to the <=> operator
in integer if applicable and returning nil otherwise. (This is how I
observed it in the Integer class [irb line 006]). I now expected the ==
operator in my class to behave exactly like the == in Integer. This
works fine, while it is applicable (if the Modnums have the same base).
But if I return nil (just like Integer <=> String) <= is fine [line
009], but == behaves differently from what I expected. Why doesn't [line
11] return false like [line 10] does? I thought == is supposed to return
true or false, nothing else?
Thanks for your help,
Manuel
----------------------
irb(main):001:0> require "modnum.rb"
=> true
irb(main):002:0> a = 5
=> 5
irb(main):003:0> b = "Hallo"
=> "Hallo"
irb(main):004:0> c = Modnum.new(3,7)
=> #<Modnum:0x101ae1c8 @base=7, @value=3>
irb(main):005:0> d = Modnum.new(3,9)
=> #<Modnum:0x101a6c20 @base=9, @value=3>
irb(main):006:0> a <=> b
=> nil
irb(main):007:0> c <=> d
=> nil
irb(main):008:0> a <= b
ArgumentError: comparison of Fixnum with String failed
from (irb):8:in `<='
from (irb):8
irb(main):009:0> c <= d
ArgumentError: comparison of Modnum with Modnum failed
from (irb):9:in `<='
from (irb):9
irb(main):010:0> a == b
=> false
irb(main):011:0> c == d
=> nil