我有一个哈希数组@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
我如何搜索这个数组,并返回一个哈希数组,其中一个块返回真?
例如:
@fathers.some_method("age" > 35) #=> array containing the hashes of bob and batman
谢谢。
如果你的数组看起来像
array = [
{:name => "Hitesh" , :age => 27 , :place => "xyz"} ,
{:name => "John" , :age => 26 , :place => "xtz"} ,
{:name => "Anil" , :age => 26 , :place => "xsz"}
]
你想知道数组中是否已经存在某个值。使用查找方法
array.find {|x| x[:name] == "Hitesh"}
如果Hitesh在name中存在,则返回object,否则返回nil
如果你的数组看起来像
array = [
{:name => "Hitesh" , :age => 27 , :place => "xyz"} ,
{:name => "John" , :age => 26 , :place => "xtz"} ,
{:name => "Anil" , :age => 26 , :place => "xsz"}
]
你想知道数组中是否已经存在某个值。使用查找方法
array.find {|x| x[:name] == "Hitesh"}
如果Hitesh在name中存在,则返回object,否则返回nil