Provides summation operation for Hash
and OpenStruct
Works with nested structures
Add sumhash
to your Gemfile:
gem "sumhash"
Hash
es with same keys
{ one: 1.0, two: 2.3 } + { one: 2.0, two: 2.0 }
Result:
{ one: 3.0, two: 4.3 }
With different keys
{ one: 1.1, two: 2.3, three: 3.4 } + { two: 3.2, four: 4.5 } + { one: 1.0, two: 2.0 }
Result:
{ one: 2.1, two: 7.5, three: 3.4, four: 4.5 }
Hash
es with nested OpenStruct
hashes = [
{ one: 1, two: 2, resume: OpenStruct.new(result: 10) },
{ two: 2, resume: OpenStruct.new(result: 20) }
]
hashes.reduce(:+)
Result:
{ one: 1, two: 4, resume: OpenStruct.new(result: 30) }
- Fork it ( https://github.com/shhavel/sumhash/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request