Skip to content

Instantly share code, notes, and snippets.

@vagnerzampieri
Last active December 14, 2015 08:38
Show Gist options
  • Select an option

  • Save vagnerzampieri/5058908 to your computer and use it in GitHub Desktop.

Select an option

Save vagnerzampieri/5058908 to your computer and use it in GitHub Desktop.
Inject hash and sum.
b = {"foo" => 100, "bar" => 300, "zaz" => 5}
c = {"foo" => 200, "bar" => 8}
a = [b, c]
a.inject({}) { |memo, i| memo.merge(i) { |key, first, second| first + second } }
=> {
"foo" => 300,
"bar" => 308,
"zaz" => 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment