Skip to content

Instantly share code, notes, and snippets.

@hasclass
Created September 21, 2009 09:20
Show Gist options
  • Save hasclass/190152 to your computer and use it in GitHub Desktop.
Save hasclass/190152 to your computer and use it in GitHub Desktop.
# Group an array into a Hash of Arrays
#
# [<Obj: name, category>,<Obj: name, category>,<Obj: name, category>]
#
# => {'category_1' => [<Obj: name, category>], 'category_2' => [<Obj: name, category>,<Obj: name, category>]}
arr.inject({}) {|hsh,obj| hsh[obj.category] ||= []; hsh[obj.category] << obj; hsh}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment