Ruby Hash delete()函数
delete()是一个Hash类方法,用于删除键值对,并返回键值等于键值的Hash值。
语法。Hash.delete()
参数:哈希数组
返回:键值等于被删除键值的哈希值。
例子 #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"
输出:
delete a : 100
delete b : 100
例子#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"
输出:
delete c : 200
delete c : xy not found