From: Josh Cheek Date: 2010-09-09T12:23:42+09:00 Subject: Re: Key Associated w/ Maximum Value in Hash --001485f86ddef7a2c4048fcb27b5 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Sep 8, 2010 at 9:57 PM, Timothy Baron wrote: > Simple question: what's the cleanest way to retrieve a key associated > with the maximum value of a hash. For instance, how do I retrieve 'b' > from the following example: > > hash = Hash.new > hash['a']=4 > hash['b']=10 > hash['c']=7 > -- > Posted via http://www.ruby-forum.com/. > > How about Enumerable#max hash = {'a'=>4,'b'=>10,'c'=>7} max_record = hash.max { |a,b| a.last <=> b.last } # => ["b", 10] max_index = max_record && max_record.first # => "b" --001485f86ddef7a2c4048fcb27b5--