Skip to content

Instantly share code, notes, and snippets.

@stephenjudkins
Created May 22, 2009 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenjudkins/116239 to your computer and use it in GitHub Desktop.
Save stephenjudkins/116239 to your computer and use it in GitHub Desktop.
def self.group_by(method_name)
define_method :"group_by_#{method_name}" do
inject([]) do |_, v|
k = yield(v)
if _.empty? || !_.last.send(method_name, k)
_ << [v]
else
_.last << v
end
_
end
end
end
group_by :first # --> def group_by_first
group_by :includ? # --> def group_by_include?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment