红宝石 |哈希删除()函数
delete()是一个 Hash 类方法,它删除键值对并从 key 等于 key 的 hash 中返回值。
Syntax: Hash.delete()
Parameter: Hash array
Return: value from hash whose key is equal to deleted key.
示例 #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