📜  红宝石 |哈希大小()方法

📅  最后修改于: 2022-05-13 01:54:52.699000             🧑  作者: Mango

红宝石 |哈希大小()方法

Hash#size()是一个 Hash 类方法,它返回哈希中键值对的计数。

示例 #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