# Why doesn't Array include Comparable

**URL:** <https://www.ruby-forum.com/t/why-doesnt-array-include-comparable/102000>\
**Category:** Ruby\
**Created:** [August 9, 2006, 6:16am UTC](https://www.ruby-forum.com/t/why-doesnt-array-include-comparable/102000 "2006-08-09T06:16:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 9, 2006, 6:16am UTC](https://www.ruby-forum.com/t/why-doesnt-array-include-comparable/102000/1 "2006-08-09T06:16:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 9, 2006, 6:58am UTC](https://www.ruby-forum.com/t/why-doesnt-array-include-comparable/102000/2 "2006-08-09T06:58:13Z")

</div>

removed\_email\_address@domain.invalid wrote:

> 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 9, 2006, 2:25pm UTC](https://www.ruby-forum.com/t/why-doesnt-array-include-comparable/102000/3 "2006-08-09T14:25:35Z")

</div>

On Aug 9, 2006, at 12:56 AM, Hal F. wrote:

> > true  
> > those would not make sense anyway.

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 9, 2006, 3:07pm UTC](https://www.ruby-forum.com/t/why-doesnt-array-include-comparable/102000/4 "2006-08-09T15:07:41Z")

</div>

Indeed I have found it useful to

class Tuple \< Array  
include Comparable  
end

But I also make it immutable.

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 9, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/why-doesnt-array-include-comparable/102000/5 "2006-08-09T14:52:40Z")

</div>

On Aug 9, 2006, at 7:22 AM, Logan C. wrote:

> But spaceship is already defined for array (and it does the right  
> thing ™). 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 G. II

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 9, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/why-doesnt-array-include-comparable/102000/6 "2006-08-09T16:13:47Z")

</div>

On Wed, 9 Aug 2006, Hal F. 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 9, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/why-doesnt-array-include-comparable/102000/7 "2006-08-09T16:49:50Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > those would not make sense anyway.
> 
> 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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 9, 2006, 4:58pm UTC](https://www.ruby-forum.com/t/why-doesnt-array-include-comparable/102000/8 "2006-08-09T16:58:52Z")

</div>

On Wed, 9 Aug 2006, Logan C. wrote:

> But spaceship is already defined for array (and it does the right thing  
> ™). 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
