Hash#empty?()是一種Hash類方法,用於檢查Hash數組是否具有任何鍵值對。
用法:Hash.empty?()
參數:散列值
返回:true-如果沒有鍵值對,否則返回false
範例1:
# Ruby code for Hash.empty?() method
# declaring Hash value
a = {a:100, b:200}
# declaring Hash value
b = {a:100, c:300, b:200}
# declaring Hash value
c = {a:100}
# empty? Value
puts "Hash a empty? form:#{a.empty?()}\n\n"
puts "Hash b empty? form:#{b.empty?()}\n\n"
puts "Hash c empty? form:#{c.empty?()}\n\n"
輸出:
Hash a empty? form:false Hash b empty? form:false Hash c empty? form:false
範例2:
# Ruby code for Hash.empty?() method
# declaring Hash value
a = { "a" => 100, "b" => 200 }
# declaring Hash value
b = {}
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
# emoty? Value
puts "Hash a empty? form:#{a.empty?()}\n\n"
puts "Hash b empty? form:#{b.empty?()}\n\n"
puts "Hash c empty? form:#{c.empty?()}\n\n"
輸出:
Hash a empty? form:false Hash b empty? form:true Hash c empty? form:false
相關用法
- Ruby Hash dig()用法及代碼示例
- Ruby Hash each()用法及代碼示例
- Ruby Hash eql?用法及代碼示例
- Ruby Hash any?()用法及代碼示例
- Ruby Hash key()用法及代碼示例
- Ruby Hash key?()用法及代碼示例
- Ruby Set empty?()用法及代碼示例
- Ruby Hash delete()用法及代碼示例
- Ruby Hash delete_if()用法及代碼示例
- Ruby Hash values用法及代碼示例
- Ruby Hash each_key用法及代碼示例
- Ruby Hash fetch用法及代碼示例
- Ruby Hash fetch_values()用法及代碼示例
- Ruby Hash flatten()用法及代碼示例
- Ruby Hash rassoc()用法及代碼示例
注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 Ruby | Hash empty? function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。