Skip to content

Instantly share code, notes, and snippets.

@olly
Created April 6, 2009 12:02
Show Gist options
  • Save olly/90730 to your computer and use it in GitHub Desktop.
Save olly/90730 to your computer and use it in GitHub Desktop.
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