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

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -