Open In App
Related Articles

Ruby | Hash delete_if() function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
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"

                    
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"

                    
Output :
delete   b : {"c"=>300}


Last Updated : 07 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads