Deleting a hash from array of hashes in Ruby -


i have array of hashes following:

[{"k1"=>"v1", "k2"=>"75.1%"}, {"k1"=>"v2", "k2"=>"-na-"}, {"k1"=>"v3", "k2"=>"5.1%"}] 

now, want first check whether array contains hash key "k1" value "v3". if yes, want delete hash array.

the result should be:

[{"k1"=>"v1", "k2"=>"75.1%"}, {"k1"=>"v2", "k2"=>"-na-"}] 

use array#delete_if:

arr = [{"k1"=>"v1", "k2"=>"75.1%"}, {"k1"=>"v2", "k2"=>"-na-"}, {"k1"=>"v3", "k2"=>"5.1%"}]  arr.delete_if { |h| h["k1"] == "v3" } #=> [{"k1"=>"v1", "k2"=>"75.1%"}, {"k1"=>"v2", "k2"=>"-na-"}] 

if there no hash matching condition, array left unchanged.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -