Using inject with an array of hashes

ruby

Solution

If you don't specify an argument to `inject`, the value for the memo object for the first iteration is the first element of the enumerable, an hash in this case. So you just have to pass `0` as the argument to `inject`:

array = [{lol: 1}, {lol: 2}]
array.inject(0) { |sum, h| sum + h[:lol] }
# => 3

Problem

I have an array of hashes, each with a key `lol` which has an integer value. I'd like to sum the values, inject always worked but now I get an exception: ``` array = [{lol: 1}, {lol: 2}] array.inject {|memo, (key, value)| memo + value} => NoMethodError: undefined method `+' for {:lol=>1}:Hash from (irb):26:in `block in irb_binding' from (irb):26:in `each' from (irb):26:in `inject' from (irb):26 ``` Por que?

Original source