Ruby Hash values_at方法
Hash#values_at()是一个Hash类方法,它返回包含键值的数组。
语法。Hash.values_at()
参数:Hash values_at
返回:包含键值对应的数组。
例子 #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]