While translating the book "Programming Ruby" by Dave Thomas and
Andrew Hunt into german language (see
http://home.vr-web.de/juergen.katins/ruby/) I stumbled arcoss an
irritating inconsistence in the standard library. In the class complex
there is a comparision operator <=>.
<=> ref <=> other -> -1, 0, +1
Returns ref.abs <=> other.abs.
In mathematical meanings this makes no sense! You can not compare
complex numbers! This is due to the following reason:
Normally you would expect a comparision to be compatible with
addition. You expect:
If a > b then a+c > b+c.
This rule can not be accomplished with this compare method. Example:
(0 + 2i) > (1 + 0i) is true, but
(0 + 2i)+(2 + 0i) > (1 + 0i)+(2 + 0i) is false!
This wrongly comparision method leeds to the need of an additional
equation operator ==, also something you would not expect. If you see
a=b you would expect the two numbers to be the same, not to have the
same length.
Following the principle of least surprise I would strongly recommend
for the next version of ruby to discard the complex operators <=> and
==, leaving only the operator = which should work as you would expect.
PS: Do not try to invent a comparision which is compatible with
addition. This is possible but then the comparision will not be
compatible with multiplication.
Regards
Jgen Katins