delete_if()是一種哈希類方法,如果塊條件為true,則刪除鍵-值對
用法:Hash.delete_if()
參數:哈希數組
阻止條件
返回: key 等於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}
相關用法
- Ruby Hash any?()用法及代碼示例
- Ruby Hash dig()用法及代碼示例
- Ruby Hash key()用法及代碼示例
- Ruby Hash key?()用法及代碼示例
- Ruby Hash each()用法及代碼示例
- Ruby Hash eql?用法及代碼示例
- Ruby Hash empty?用法及代碼示例
- Ruby Hash each_key用法及代碼示例
- Ruby Hash values用法及代碼示例
- Ruby Regexp hash()用法及代碼示例
- Ruby Hash each_key()用法及代碼示例
- Ruby Time hash用法及代碼示例
- Ruby Hash fetch用法及代碼示例
- Ruby Hash flatten()用法及代碼示例
- Ruby Hash compare_by_identity?()用法及代碼示例
注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 Ruby | Hash delete_if() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。