Ruby | Hash delete_if() function
delete_if() is a Hash class method which delete_if the key-value pair if the block condition is true
Syntax: Hash.delete_if()
Parameter: Hash array
Block Condition
Return: value from hash whose key is equal to delete_ifd key.
Example #1:
# Ruby code for delete_if() method # declaring Hash value a = { "a" => 100, "b" => 200 } # declaring Hash value b = {"a" => 100, "c"=>30} puts "delete_if a : #{a.delete_if{|key, value| value > 200 } }\n\n" puts "delete_if b : #{b.delete_if{|key, value| key == "a" } }\n\n" |
chevron_right
filter_none
Output :
delete_if a : {"a"=>100, "b"=>200}
delete_if b : {"c"=>30}
Example #2:
# Ruby code for delete_if() method # declaring Hash value c = {"a" => 100, "c" => 300, "b" => 200} puts "delete b : #{c.delete_if{|key, value| key <= "b" } }\n\n" |
chevron_right
filter_none
Output :
delete b : {"c"=>300}
Recommended Posts:
- Ruby | Hash any?() function
- Ruby | Hash key?() function
- Ruby | Hash dig() function
- Ruby | Hash eql? function
- Ruby | Hash each() function
- Ruby | Hash key() function
- Ruby | Hash compact!() function
- Ruby | Hash compact() function
- Ruby | Hash clear() function
- Ruby | Hash fetch function
- Ruby | Hash empty? function
- Ruby | Hash each_key function
- Ruby | Hash assoc() function
- Ruby | Hash rassoc() function
- Ruby | Hash each_key() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



