Hi Robert, On 19/01/12 23:26, Robert Klemme wrote: >>> foo.sort_by {|a| >>> [ >>> # Ascending, primary. >>> a.key0, >>> # Descending, secondary, only if numeric. >>> -a.key1, >>> # A tricky and expensive comparison, tertiary. >>> a.create_magic_key >>> ] >>> } >> >> I'd stumbled on sort_by initially, I suspect I'll use it when I can >> precompute the comparisons easily (this is sometimes the case, sometimes >> not). > > Note, with #sort_by you do not precompute comparisons but you compute > a comparison key! In the example it is an Array because Array#<=> > uses fields from the beginning to the end, i.e. sorts according to > fields with decreasing precedence (first pos 0, if that comparison > returns 0 field pos 1 etc.). I just reread my previous response and my explanation was really poor- sorry about that. Thankyou for the clarification, it matches my understanding, and expresses it far better than I did. :) >> I'm not at all familiar with the "enum.sort_by&YourClass::SORT_FOO" syntax >> at this point, so I might need to read up on that. I can at least partly >> guess from the context though. > > It just passes the lambda as a block as if it the block was passed > directly. Note, that technically behind the scenes the syntax foo(&x) > invokes x.to_proc which is also provided by Symbol: > > irb(main):001:0> a = 5.times.to_a > => [0, 1, 2, 3, 4] > irb(main):002:0> a.map {|x| x.to_s} > => ["0", "1", "2", "3", "4"] > irb(main):003:0> a.map(&:to_s) > => ["0", "1", "2", "3", "4"] Excellent, thankyou. :) The additional example solidified my understanding of it too. Thankyou for taking the additional time to explain this, much appreciated. :) Garth