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:
a = { "a" => 100, "b" => 200 }
b = {"a" => 100}
puts "delete a : #{a.delete("a") }\n\n"
puts "delete b : #{b.delete("a") }\n\n"
|
Output :
delete a : 100
delete b : 100
Example #2:
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"
|
Output :
delete c : 200
delete c : xy not found