Delete if for a certain value : delete_if « Hash « Ruby






Delete if for a certain value


myHash = { 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four", 5 => "Five" }

myHash.delete_if { |key, value| value == "Two" } # => {5=>"Five", 1=>"One", 3=>"Three", 4=>"Four"}

 








Related examples in the same category

1.Deleting Hash Elements Conditionally
2.Delete if with block logics
3.delete_if uses a block
4.delete_if passes all pairs into the block, so you can delete based on a key or a value.