Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

summing numbers in a hash in ruby

Tags:

ruby

I have a hash like this.

products = {199 =>['Shoes', 59.99], 211 =>['Shirts', 19.99], 245 =>['Hats', 25.99], 689 => ['Coats', 99.99], 712 => ['Beanies', 6.99]}

It has an item number => [product, price].

I would like to sum up all the prices without using the inject method.

Can anyone help me please?

like image 901
eddie tuell Avatar asked Feb 26 '12 23:02

eddie tuell


1 Answers

products.values.map(&:last).reduce(:+) #=> 212.95
like image 153
megas Avatar answered Sep 27 '22 23:09

megas