Created
September 21, 2009 09:20
-
-
Save hasclass/190152 to your computer and use it in GitHub Desktop.
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
# 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