views:

65

answers:

1

Hi,

I have an array of hashes, @fathers.

a_father = { "father" => "Bob", "age" =>  40 }
@fathers << a_father
a_father = { "father" => "David", "age" =>  32 }
@fathers << a_father
a_father = { "father" => "Batman", "age" =>  50 }
@fathers << a_father 

How can I search this array and return an array of hashes for which a block returns true?

For example:

@fathers.some_method("age" > 35) #=> array containing the hashes of bob and batman

Thanks.

+8  A: 

You're looking for Enumerable#select:

@fathers.select {|f| f["age"] > 35 }
# => [{"age"=>40, "father"=>"Bob"}, {"age"=>50, "father"=>"Batman"}]
Jordan
Oh! You were the first one! Deleting my answer and +1.
Milan Novota
Excellent. Many thanks!
doctororange