I have an array that looks like so:
f = [["Wed, 12-31", 120.0],["Thu, 01-01", 120.0], ["Thu, 01-01", 120.0]]
I can convert it to a hash and remove the duplicate keys:
h = Hash[ *f.collect { |v| [v] }.flatten ]
# => {"Wed, 12-31"=>120.0, "Thu, 01-01"=>120.0}
which is almost there, but I'd like to sum the value for elements with the identical date strings, the desired result from the above array would be this:
{"Wed, 12-31"=>120.0, "Thu, 01-01"=>240.0}
How can I accomplish this?
.collect.flattennonsense. Just useHash[array].f.collect { |v| [v] }is just mapping the array to itself. It doesn't do anything..flattenon an already flat array is equally useless..collectand.flatten