Skip to content

Instantly share code, notes, and snippets.

@leejarvis
Created January 25, 2012 12:32
Show Gist options
  • Save leejarvis/1676063 to your computer and use it in GitHub Desktop.
Save leejarvis/1676063 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# dont try this at home
items = [{ a: 1, b: 2 }, { a: 1, b: 3 }, { a: 4, b: 5 }]
items = items.each_with_object(Hash.new { |h, k| h[k] = [] }) { |i, o| o[i[:a]] << i[:b] }.map { |k, v| [k, v.join(', ')] }
p items #=> [[1, "2, 3"], [4, "5"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment