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