📜  红宝石 |哈希删除()函数

📅  最后修改于: 2022-05-13 01:54:29.629000             🧑  作者: Mango

红宝石 |哈希删除()函数

delete()是一个 Hash 类方法,它删除键值对并从 key 等于 key 的 hash 中返回值。

示例 #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