Kern Twente Kern Twente - 1 year ago 65
Ruby Question

Find in a hash any values greater then a specific value

Suppose I have this hash

{ 1 => 4 , 2 => 3 , 4 => 1}

Now I want to make a loop where I can find any value which is greater then 3.

I know with
I can find the pairs has a value of 3.
But how can I find all the values which are greater of equal as 3.


Edit 1 :
I try to make this pseudo code at work in ruby.

while there are sets of 3 ones:
remove the set from the hash (h[1] -= 3)
sum 1000 to the score

Hck Hck
Answer Source

Use #select method:

{a: 1, b: 2, c: 3}.select{|k,v| v > 1}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download