Skip to content
Related Articles

Related Articles

Ruby | Hash values_at method
  • Last Updated : 07 Jan, 2020

Hash#values_at() is a Hash class method which returns the array containing the values corresponding to keys.

Syntax: Hash.values_at()

Parameter: Hash values_at

Return: array containing the values corresponding to keys.

Example #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"

Output :

Hash a values_at form : [nil]

Hash b values_at form : [nil]

Hash c values_at form : [nil]

Example #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"

Output :

Hash a values_at form : [100]

Hash b values_at form : [nil]

Hash c values_at form : [200]


My Personal Notes arrow_drop_up
Recommended Articles
Page :