[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [Q] Array not Comparable?

Warren Brown

10/22/2003 3:52:00 PM

Guy,

> svg% ruby -e 'p ([1, "a"] <=> [1, 2])'
> nil
> svg%

I see your point, but:

%ruby -e 'p [[1],[2]].sort'

[[1], [2]]
%ruby -e 'p [[1],["a"]].sort'
-e:1:in `sort': comparison of Array with Array failed (ArgumentError)
from -e:1

So why have Array#sort allow comparisons where possible (throwing an
error where it is not), but not allow comparison operators to do the
same?

irb(main):001:0> class Array
irb(main):002:1> include Comparable
irb(main):003:1> end
=> Array
irb(main):004:0> [1] < [2]
=> true
irb(main):005:0> [1] < ['a']
ArgumentError: comparison of Array with Array failed
from (irb):5:in `<'
from (irb):5

This seems more consistent to me. Anyone else?

- Warren Brown



1 Answer

ts

10/22/2003 3:57:00 PM

0

>>>>> "W" == Warren Brown <wkb@airmail.net> writes:

W> So why have Array#sort allow comparisons where possible (throwing an
W> error where it is not), but not allow comparison operators to do the
W> same?

ruby give you the possibility to sort an array, but it don't say that 2
arrays are comparable, precisely because it exist case when it can't
compare 2 arrays.


W> This seems more consistent to me. Anyone else?

See [ruby-talk:84370]



Guy Decoux