Berger, Daniel
8/9/2006 2:49:00 PM
ara.t.howard@noaa.gov wrote:
> On Wed, 9 Aug 2006, Hal Fulton wrote:
>
>> Offhand I would guess it's because arrays can
>> hold arbitrary objects.
>>
>> If one array held four different classes of
>> objects, and another held yet another four
>> classes of objects, that's potentially sixteen
>> spaceship operators that would need to be
>> defined for this to make sense. And most of
>> those would not make sense anyway.
>
> i don't understand - it's up to to the objects inside the container to
> impliment the right methods and array already knows how to handle it:
>
>
> harp:~ > cat a.rb
> a = 0, 'foo', 42
> b = 1, 'bar', 42.0
>
> p a < b rescue puts "can't do that"
>
> class Array; include Comparable; end
>
> p a < b
>
> p a.sort_by{ rand } < b.sort_by{ rand }
>
>
> harp:~ > ruby a.rb
> can't do that
> true
> a.rb:9:in `<': comparison of Array with Array failed (ArgumentError)
> from a.rb:9
>
>
> if you're collections impliment <=> correctly then Array does the right
> thing -
> looping over each of them.
>
> -a
FWIW, I modified the Array class to include Comparable and then ran both the
test_array.rb file that's distributed with the Ruby distro and my own tests in
the ruby_test project and all tests still passed.
Regards,
Dan
This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly
prohibited and may be unlawful. If you have received this communication
in error, please immediately notify the sender by reply e-mail and destroy
all copies of the communication and any attachments.