Hash#length():length()是一種Hash類方法,它給出哈希中的鍵值對數量。
用法:Hash.length()
參數:散列值
返回:哈希中鍵/值對的數量
示例1:
# Ruby code for Hash.length() 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}
# length Value
puts "Hash a length form : #{a.length}\n\n"
puts "Hash b length form : #{b.length}\n\n"
puts "Hash c length form : #{c.length}\n\n"
輸出:
Hash a length form : 2 Hash b length form : 3 Hash c length form : 1
示例2:
# Ruby code for Hash.length() method
# declaring Hash value
a = { "a" => 100, "b" => 200 }
# declaring Hash value
b = {"a" => 100}
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
# length Value
puts "Hash a length form : #{a.length}\n\n"
puts "Hash b length form : #{b.length}\n\n"
puts "Hash c length form : #{c.length}\n\n"
輸出:
Hash a length form : 2 Hash b length form : 1 Hash c length form : 3
相關用法
- Ruby Hash value?用法及代碼示例
- Ruby Hash select()用法及代碼示例
- Ruby Hash replace()用法及代碼示例
- Ruby Hash shift()用法及代碼示例
- Ruby Hash to_hash用法及代碼示例
- Ruby Hash to_s用法及代碼示例
- Ruby Hash to_proc用法及代碼示例
- Ruby Hash values_at用法及代碼示例
- Ruby Hash size()用法及代碼示例
- Ruby Hash store()用法及代碼示例
- Ruby Hash reject用法及代碼示例
注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 Ruby | Hash length() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。