Kern Twente Kern Twente - 4 months ago 9
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
hash.values?3
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.

Roelof

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
end

Hck Hck
Answer

Use #select method:

{a: 1, b: 2, c: 3}.select{|k,v| v > 1}
Comments