Reputation: 81
I would like to add the values of this hash together, and output the total.
b = {"Mike"=>100, "Jim"=>20}
Upvotes: 1
Views: 106
Reputation: 118299
You do as below
hash = {"Mike"=>100, "Jim"=>20}
hash.values.reduce(:+) # => 120
# or
hash.reduce(0) { |sum,(_, v)| sum + v } # => 120
Read this powerful method Enumerable#reduce
.
Upvotes: 1