Open In App
Related Articles

Ruby | Hash values_at method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
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]




Last Updated : 07 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads