Ruby | Hash delete() function
delete() is an Hash class method which deletes the key-value pair and returns the value from hash whose key is equal to key.
Syntax: Hash.delete()
Parameter: Hash array
Return: value from hash whose key is equal to deleted key.
Example #1:
# Ruby code for delete() method # declaring Hash value a = { "a" => 100, "b" => 200 } # declaring Hash value b = {"a" => 100} puts "delete a : #{a.delete("a") }\n\n" puts "delete b : #{b.delete("a") }\n\n" |
chevron_right
filter_none
Output :
delete a : 100 delete b : 100
Example #2:
# Ruby code for delete () method # declaring Hash value c = {"a" => 100, "c" => 300, "b" => 200} puts "delete c : #{c.delete("b") }\n\n" puts "delete c : #{c.delete("xy"){ |el| "#{el} not found" } }\n\n" |
chevron_right
filter_none
Output :
delete c : 200 delete c : xy not found
Recommended Posts:
- Ruby | Hash key?() function
- Ruby | Hash key() function
- Ruby | Hash dig() function
- Ruby | Hash eql? function
- Ruby | Hash each() function
- Ruby | Hash any?() function
- Ruby | Hash compare_by_identity () function
- Ruby | Hash rassoc() function
- Ruby | Hash each_key() function
- Ruby | Hash compact!() function
- Ruby | Hash compact() function
- Ruby | Hash assoc() function
- Ruby | Hash clear() function
- Ruby | Hash compare_by_identity? () function
- Ruby | Hash invert() 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.



