Open In App

Ruby | Hash delete() function

Last Updated : 07 Jan, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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
# 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"
Output :
delete   a : 100

delete   b : 100

Example #2: Ruby
# 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"
Output :
delete   c : 200

delete   c : xy not found


Next Article

Similar Reads

  翻译: