ruby - How can I turn an array of keys into an array of values? -
i have hash like
hash = { 'burger': 2, 'kebab': 5, 'pizza': 10 }
and have array of keys like
['burger', 'burger', 'pizza']
how create array of corresponding values? inject
method best method sum value array together?
actually, don't need prepare key arrays.
- to keys:
hash.keys
- to values:
hash.values
if want values or values in order, then
a = [:burger, :pizza] hash.values_at(*a) # => [2, 10]
Comments
Post a Comment