Last active
August 29, 2015 13:56
-
-
Save kithokit/9222400 to your computer and use it in GitHub Desktop.
The use of group_by and reduce, merge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animals = [ | |
{type: "dog", a: 2}, | |
{type: "dog", b: 7}, | |
{type: "dog", c: 2}, | |
{type: "cat", d: 3}, | |
{type: "cat", e: 4}, | |
{type: "cat", f: 0}, | |
{type: "rabbit", g: 0}, | |
{type: "rabbit", h: 1} | |
] | |
animals = animals.group_by do |inner_hash| | |
inner_hash[:type] | |
end | |
puts animals.map { |_,v| puts _; v.reduce(:merge) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment