📜  红宝石 |哈希 delete_if()函数

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

红宝石 |哈希 delete_if()函数

delete_if()是一个 Hash 类方法,如果块条件为真,则 delete_if 键值对

示例 #1:

# Ruby code for delete_if() method
  
  
# declaring Hash value
a = { "a" => 100, "b" => 200 }
  
# declaring Hash value
b = {"a" => 100, "c"=>30}
  
  
puts "delete_if   a : #{a.delete_if{|key, value| value > 200 } }\n\n"
  
puts "delete_if   b : #{b.delete_if{|key, value| key == "a" } }\n\n"

输出 :

delete_if   a : {"a"=>100, "b"=>200}

delete_if   b : {"c"=>30}

示例 #2:

# Ruby code for delete_if() method
  
  
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
  
puts "delete   b : #{c.delete_if{|key, value| key <= "b" } }\n\n"

输出 :

delete   b : {"c"=>300}