James Gray
7/10/2007 1:24:00 PM
On Jul 10, 2007, at 8:13 AM, Gregory Brown wrote:
> On 7/10/07, Wayne E. Seguin <wayneeseguin@gmail.com> wrote:
>> On Jul 10, 2007, at 08:27 , Christoffer Sawicki wrote:
>> > Hello.
>> >
>> > On 7/10/07, John Carter <john.carter@tait.co.nz> wrote:
>> >> count.keys.sort_by{|key| count[key]}
>> >
>> > IMHO, the most elegant way to do this is:
>> >
>> > hash.sort_by { |k, v| v }.map { |k, v| k }
>> >
>> > ...where k = key and v = value.
>> >
>> > Cheers,
>>
>> Ack, pet peeve.
>>
>> hash.sort_by { | key, value | value } .map { | key, value | key }
>>
>> If you have to specify "where k = key and v = value" then these
>> should have been used in your code.
>>
>> Always favor readability at the expense of verbosity, both your
>> future self and whoever else maintains your code will thank you.
>
> I find myself always using |k,v|, when used on something hashlike I
> don't think readability suffers.
I've also recently adopted the trick of using _ as an unused
parameter name. I believe it was Ara that first suggested this and I
think it's a great idea:
hash.sort_by { |key, _| … }…
James Edward Gray II