From: Rick DeNatale Date: 2007-12-23T10:40:13+09:00 Subject: Re: Why is Array not Comparable On Dec 22, 2007 8:17 PM, Martin Duerst wrote: > I just bumped into this: > Why does Array not include Comparable? > Array defines <=>, and so including Comparable should be easy. > > It looks to me like a big oversight, or there must have been > some very deep reasons to not do this, which I'm completely ignorant of. > Good question. You can of course either open Array and include comparable, or extend an individual array instance. But some subtleties. 1) Just because Array implements <=> doesn't mean that it will work as you expect for all arrays. Array basically implements <=> by comparing elements with the argument until either an element comparison returns a non-zero or either the receiver or argument 'runs out' of elements. So if elements aren't comparable with <=> pairwise ... 2) Comparable not only requires that <=> be implemented but that it return -1, 0, or 1. Some classes implement it to return nil if the argument isn't of the right class, so even if Array included comparable things like ["a] < [1] wouldn't work. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/