Skip to content

Instantly share code, notes, and snippets.

@timcowlishaw
Created June 5, 2009 14:38
Show Gist options
  • Save timcowlishaw/124309 to your computer and use it in GitHub Desktop.
Save timcowlishaw/124309 to your computer and use it in GitHub Desktop.
module Enumerable
def group(&block)
groups = {}
self.each do |elem|
the_key = block ? block.call(elem) : elem
if groups[the_key]
groups[the_key].push(elem)
else
groups[the_key] = [elem]
end
end
return groups.values
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment