红宝石 |哈希 delete_if()函数
delete_if()是一个 Hash 类方法,如果块条件为真,则 delete_if 键值对
Syntax: Hash.delete_if()
Parameter: Hash array
Block Condition
Return: value from hash whose key is equal to delete_ifd key.
示例 #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}