From: Edgardo Hames Date: 2004-08-31T05:41:32+09:00 Subject: Re: Flexible operations for a collection class On Tue, 31 Aug 2004 05:25:26 +0900, Robert Klemme wrote: > > > > > but I would like to implement the #<=> method in my objects, so I can > > delegate the #sort method to @elements. I came up with this, > > > > def sortable_by=(element_attribute) > > @sortable_by=element_attribute > > @elements.each{|o| o.sortable_by=@sortable_by} > > end > > Putting the comparison criteria into the elements is very bad design. What > will you do if your elements are in two different collections at the same > time? The sorting criterium belongs into the collection. From The Pickaxe Book, in the Enumerable Module "Mix in Enumerable, and suddenly your class supports things such as map, include?, and find_all?. If the objects in your collection implement meaningful ordering semantics using the <=> method, you'll also get min, max, and sort." That's the reason behind my design. But, the Set solution satisfies most of my needs. I just need to add a few methods to it. Thanks for your clear response. Ed -- "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian W. Kernighan