input = [ {"name" => "adam", "nick" => ["kuruvi", "cali"] },
{"name" => "eve", "nick" => ["cali"] },
{"name" => "enoch", "nick" => [] },
{"name" => "ebe", "nick" => ["test", "wrong"] },
{"name" => "fred", "nick" => ["da"] } ]
我想選擇其中包含"nick"
"da"
& "cali"
哈希選擇從哈希散列數組。使用「AND」操作紅寶石
input.select do |d|
d["nick"].include?("cali")
end
給
[{"name"=>"adam", "nick"=>["kuruvi", "cali"]},
{"name"=>"eve", "nick"=>["cali"]}]
我們怎樣才能包括檢查兩個參數。例如:include?("cali","da")
讓我的結果將是
[{"name"=>"adam", "nick"=>["kuruvi", "cali"]},
{"name"=>"eve", "nick"=>["cali"]},
{"name" => "fred", "nick" => ["da"]}]
試過這種
i = ["da","cali"]
input.select do |d|
d["nick"].all? { |v| i.include? v }
end
=> [{"name" => "adam", "nick" => ["kuruvi", "cali"]},
{"name" => "eve", "nick" => ["cali"]},
{"name" => "enoch", "nick" => []},
{"name" => "fred", "nick" => ["da"]}]
(但這返回哈希有"nick"=[]
也)
嘗試這種:: I = [ 「DA」, 「卡利」] input.select做| d | d [「nick」]。all? {| v | i.include? v} end – user3636388
上面的返回{「name」=>「enoch」,「nick」=> []}也..其中「nick」是[] – user3636388
'all?'調用不在正確的位置那裏。這是行不通的。 – tadman