红宝石 |哈希值_at方法
Hash#values_at()是一个 Hash 类方法,它返回包含与键对应的值的数组。
Syntax: Hash.values_at()
Parameter: Hash values_at
Return: array containing the values corresponding to keys.
示例 #1:
# Ruby code for Hash.values_at() 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}
# values_at Value
puts "Hash a values_at form : #{a.values_at("a")}\n\n"
puts "Hash b values_at form : #{b.values_at("d")}\n\n"
puts "Hash c values_at form : #{c.values_at("b")}\n\n"
输出 :
Hash a values_at form : [nil]
Hash b values_at form : [nil]
Hash c values_at form : [nil]
示例 #2:
# Ruby code for Hash.values_at() 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}
# values_at Value
puts "Hash a values_at form : #{a.values_at("a")}\n\n"
puts "Hash b values_at form : #{b.values_at("d")}\n\n"
puts "Hash c values_at form : #{c.values_at("b")}\n\n"
输出 :
Hash a values_at form : [100]
Hash b values_at form : [nil]
Hash c values_at form : [200]