[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Maximum value of hash

Bart Braem

9/12/2006 2:58:00 PM

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?

Bart
9 Answers

Farrel Lifson

9/12/2006 3:08:00 PM

0

On 12/09/06, Bart Braem <bart.braem@gmail.com> 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?
>
> Bart
>
>

hash.values.max

Farrel

Trans

9/12/2006 3:10:00 PM

0


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?
>
> Bart

hash.values.max

T.


Farrel Lifson

9/12/2006 3:11:00 PM

0

On 12/09/06, Bart Braem <bart.braem@gmail.com> 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?
>
> Bart
>
>

As for sum you can use inject:
has.values.inject{|sum,value| sum + value}

Farrel

William Crawford

9/12/2006 3:18:00 PM

0

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?
>
> Bart

hash = Hash.new
hash['a']=4
=> 4
hash['b']=10
=> 10
hash['c']=7
=> 7
hash.max
=> ["c", 7]
hash.values.max
=> 10

That?

--
Posted via http://www.ruby-....

Bart Braem

9/12/2006 3:54:00 PM

0

Farrel Lifson wrote:

> On 12/09/06, Bart Braem <bart.braem@gmail.com> 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?
>>
>> Bart
>>
>>
>
> 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...

Bart

Just Another Victim of the Ambient Morality

9/12/2006 6:04:00 PM

0


"Bart Braem" <bart.braem@gmail.com> wrote in message
news:ee6l7b$7nf$1@ikaria.belnet.be...
> Farrel Lifson wrote:
>
>> On 12/09/06, Bart Braem <bart.braem@gmail.com> 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...


Rick DeNatale

9/12/2006 9:07:00 PM

0

On 9/12/06, Just Another Victim of the Ambient Morality
> Just in case you didn't realise this, the Array class (and, thus, _all_
> arrays) has the max method.

Actually the Enumerable mixin module implements max, so any enumerable
has it, and gives you the maximum element yielded by each

(1..10).max #=> 10
{'a' => 1, 'b' => 7}.max #=> ["b", 7]
"abc\ndef".max #=> "def"




--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

Marcelo Alvim

9/13/2006 12:03:00 AM

0

> Actually the Enumerable mixin module implements max, so any enumerable
> has it, and gives you the maximum element yielded by each
>
> (1..10).max #=> 10
> {'a' => 1, 'b' => 7}.max #=> ["b", 7]
> "abc\ndef".max #=> "def"

This is really sweet, but I think in this context it's worth noting that:

{'a' => 1, 'b' => 10, 'z' => 5}.max #=> ["z", 5]

As the thread is about hashes, it's nice to know that the #max method,
when applied to hashes, apparently yields the maximum value for the
keys, not for the values.

Alvim.

Bart Braem

9/13/2006 8:04:00 AM

0

Just Another Victim of the Ambient Morality wrote:

> 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...

I did realize that the Array class has the max method. After some browsing
to the documentation I also understand the inclusion of Enumerable and its
effects. I must say, my code gets really clean this way.

I'm starting to like ruby a lot. Yesterday I had to prototype an algorithm
and with Ruby I did that in a quarter of an hour. That's really fast! My
colleagues were surprised about the code, they knew how to read it without
knowing ruby!

Impressed a lot,
Bart