From: Ian Macdonald Date: 2003-08-07T02:26:35+09:00 Subject: Re: What's New and Shiny in Ruby 1.8.0? On Wed 06 Aug 2003 at 10:17:44 +0900, Harry Ohlsen wrote: > This would be particularly useful if the data items in the enumeration were > instances of more complex objects. Then, you could do things like: > > sorted = people.sort_by { |p| [p.last_name, p.first_name, p.salary] } How does one use this with one's own objects, though? I just cooked up an example Person class to play with and then tried to sort it as above: class Person include Enumerable attr_accessor :first_name, :last_name, :age def initialize(f,l,a) @first_name = f @last_name = l @age = a.to_i end end p [d, g, i, j, p, j2, j3].sort_by { |x| [x.last_name, x.first_name, x.age] } and I get: ./sort_by:23: undefined method `[]' for # (NoMethodError) I'm fine with implementing Person#[], but I can't imagine how it should look. Ian -- Ian Macdonald | If God wanted us to have a President, He System Administrator | would have sent us a candidate. -- Jerry ian@caliban.org | Dreshfield http://www.caliban.org | |