[Ironruby-core] Compare Patch

Peter Bacon Darwin bacondarwin at googlemail.com
Mon Nov 12 06:53:24 EST 2007


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
 
  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/ironruby-core/attachments/20071112/3d4c6c60/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ComparePatch.patch
Type: application/octet-stream
Size: 12385 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/ironruby-core/attachments/20071112/3d4c6c60/attachment-0001.obj 


More information about the Ironruby-core mailing list