From: Brian Candler Date: 2011-04-26T19:08:41+09:00 Subject: Re: Using the spaceship operator RichardOnRails wrote in post #994401: > While I'm still playing with delegation, I've taken "ownership" of a > couple of the ideas previously offered, most notably "sort_by" and: > class Meta < Array > def <=>(other) > self[1] <=> other[1] > end > end > > a = [] > [ [:A,12], [:B,30], [:C,4] ]. each { |element| a << > Meta.new(element) } > p a.sort And a slightly tidier way is to use 'map' (aliased as 'collect') which iterates and accumulates the output array in one go: a = [ [:A,12], [:B,30], [:C,4] ].map { |element| Meta.new(element) } p a.sort Regards, Brian. -- Posted via http://www.ruby-forum.com/.