why doesn't Array include Comparable

A

ara.t.howard

why __wouldn't__ one want this?



harp:~ > cat a.rb
a = 0,1,2
b = 2,3,4

p a < b rescue puts "can't do that!"

class Array; include Comparable; end

p a < b


harp:~ > ruby a.rb
can't do that!
true

??


-a
 
H

Hal Fulton

why __wouldn't__ one want this?



harp:~ > cat a.rb
a = 0,1,2
b = 2,3,4

p a < b rescue puts "can't do that!"

class Array; include Comparable; end

p a < b


harp:~ > ruby a.rb
can't do that!
true

??

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.


Hal
 
L

Logan Capaldo

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.

But spaceship is already defined for array (and it does the right
thing (tm)). If Array is going to make spaceship available, why _not_
include Comparable?
 
J

James Edward Gray II

But spaceship is already defined for array (and it does the right
thing (tm)). If Array is going to make spaceship available, why
_not_ include Comparable?

I have to agree. I know I've wanted it a couple of times now.

James Edward Gray II
 
T

Trans

Indeed I have found it useful to

class Tuple < Array
include Comparable
end

But I also make it immutable.

T.
 
A

ara.t.howard

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
 
A

ara.t.howard

But spaceship is already defined for array (and it does the right thing
(tm)). If Array is going to make spaceship available, why _not_ include
Comparable?

yeah - exactly. by implimenting <=> it __is__ comparable in that one can
indeed to

a = 0,1,2
b = 2,3,4

lt = (a <=> b) == -1
eq = (a <=> b) == 0
gt = (a <=> b) == 1

so it's just anoying that i can't acutally say

lt = a < b

2 cts.

-a
 
D

Daniel Berger

On Wed, 9 Aug 2006, Hal Fulton wrote:
=20
=20
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:
=20
=20
harp:~ > cat a.rb
a =3D 0, 'foo', 42
b =3D 1, 'bar', 42.0
=20
p a < b rescue puts "can't do that"
=20
class Array; include Comparable; end
=20
p a < b
=20
p a.sort_by{ rand } < b.sort_by{ rand }
=20
=20
harp:~ > ruby a.rb
can't do that
true
a.rb:9:in `<': comparison of Array with Array failed (ArgumentError)
from a.rb:9
=20
=20
if you're collections impliment <=3D> correctly then Array does the = right=20
thing -
looping over each of them.
=20
-a

FWIW, I modified the Array class to include Comparable and then ran both =
the=20
test_array.rb file that's distributed with the Ruby distro and my own =
tests in=20
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=20
prohibited and may be unlawful. If you have received this communication =

in error, please immediately notify the sender by reply e-mail and =
destroy=20
all copies of the communication and any attachments.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top