Ruby Hash size()方法
Hash#size()是一个Hash类方法,用于返回散列中键值对的数量。
语法。Hash.size()
参数:哈希值
返回:散列中的键值对的数量。
例子 #1 :
# Ruby code for Hash.size() 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}
# size Value
puts "Hash a size form : #{a.size}\n\n"
puts "Hash b size form : #{b.size}\n\n"
puts "Hash c size form : #{c.size}\n\n"
输出:
Hash a size form : 2
Hash b size form : 3
Hash c size form : 1
例子 #2 :
# Ruby code for Hash.size() 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}
# size Value
puts "Hash a size form : #{a.size}\n\n"
puts "Hash b size form : #{b.size}\n\n"
puts "Hash c size form : #{c.size}\n\n"
输出:
Hash a size form : 2
Hash b size form : 1
Hash c size form : 3