From: Rick DeNatale Date: 2010-09-09T20:52:39+09:00 Subject: Re: Key Associated w/ Maximum Value in Hash On Thu, Sep 9, 2010 at 2:25 AM, Robert Klemme wrote: > Here's a version with #inject > > irb(main):004:0> hash.inject([nil,0]) {|(maxk,maxv),(k,v)| v > maxv ? [k,v] > : [maxk,maxv]}.last > => 10 But that fails if there are only negative values in the hash. hash = {:a => -2, :b => -4} hash.inject([nil,0]) {|(maxk,maxv),(k,v)| v > maxv ? [k,v] : [maxk,maxv]}.last # => 0 -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale