From: Just Another Victim of the Ambient Morality Date: 2006-09-13T03:05:49+09:00 Subject: Re: Maximum value of hash "Bart Braem" wrote in message news:ee6l7b$7nf$1@ikaria.belnet.be... > Farrel Lifson wrote: > >> On 12/09/06, Bart Braem wrote: >>> A very simple question: what's the best way to get the maximum value of >>> a >>> hash? Currently I have something like: >>> >>> max = 0 >>> hash.each_value do |value| >>> if (value > max ) >>> max = value >>> end >>> end >>> >>> But it does not feel ruby... Same question for sum, but I guess the >>> answer will be similar? >> >> hash.values.max >> > Thanks Farrel (and all others) for the fast replies. It didn't occur with > me > that the values array had this method of course... Just in case you didn't realise this, the Array class (and, thus, _all_ arrays) has the max method. If you wanted the max of two values, you'd put those two values in an array and then take the max of that array... a = 2 b = 3 max = [a, b].max Since Hash#values returns an _array_ of hash values...