Ruby Hash delete_if()函数
delete_if()是一个Hash类方法,如果块条件为真,则删除键值对。
语法。Hash.delete_if()
参数:哈希数组
区块条件
返回:键值等于delete_ifd键的哈希值。
例子 #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}