Created
April 6, 2009 12:02
-
-
Save olly/90730 to your computer and use it in GitHub Desktop.
This file contains 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
module Enumerable | |
# group_by method included in Ruby 1.9 | |
def group_by | |
h = {} | |
each do |o| | |
key = yield(o) | |
if h.key?(key) | |
h[key] << o | |
else | |
h[key] = [o] | |
end | |
end | |
h | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment