Skip to content

Instantly share code, notes, and snippets.

@kithokit
Last active August 29, 2015 13:56
Show Gist options
  • Save kithokit/9222400 to your computer and use it in GitHub Desktop.
Save kithokit/9222400 to your computer and use it in GitHub Desktop.
The use of group_by and reduce, merge
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