Following on from the discussions about the <=> operator and the Range
operator. The situation in the MRI is not consistent. There are some
cases
where an exception is thrown and some where nil is returned and in those
cases sometimes this is converted to false. Matz accepted this a while
back
(HYPERLINK
"http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/165155"http://b
lade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/165155) and I don’t
see
any change in the later releases.
Here is a quick patch I knocked up that tries to map to the MRI
behaviour.
The FixnumOps now has an overload on Compare that can take an object on
the
rhs and returns an object rather than int. This allows it to return
null if
the value is not compatible.
The Protocols.Compare now can return null if the comparison was not
valid.
There is a new Protocols.CheckedCompare that will return an int (as
before)
or throw an ArgumentException otherwise.
The RangeOps.CaseEquals now checks for null from the <=> operator and
returns false in that case.
This had a knock on effect across a number of classes including
Comparable,
IListOps, ArrayOps and Enumerable, which have been changed either to
explicitly cast to int (since it is guaranteed that it will be
compartible)
or to call CheckedCompare instead of Compare.
Pete
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.503 / Virus Database: 269.15.29/1124 - Release Date:
11/11/2007
10:12