Skip to content

Commit

Permalink
Merge pull request #575 from kostya/group_by
Browse files Browse the repository at this point in the history
# enumerable#group_by
  • Loading branch information
alex committed Apr 7, 2013
2 parents 62d233b + a1c3a14 commit 1fb065c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
11 changes: 11 additions & 0 deletions lib-topaz/enumerable.rb
Expand Up @@ -253,4 +253,15 @@ def none?(&block)
end
true
end

def group_by(&block)
return self.enum_for(:group_by) unless block
h = {}
self.each do |e|
v = yield e
a = h.fetch(v) { |v| h[v] = [] }
a << e
end
h
end
end
3 changes: 0 additions & 3 deletions spec/tags/core/enumerable/group_by_tags.txt
@@ -1,4 +1 @@
fails:Enumerable#group_by returns a hash with values grouped according to the block
fails:Enumerable#group_by returns an empty hash for empty enumerables
fails:Enumerable#group_by returns an Enumerator if called without a block
fails:Enumerable#group_by gathers whole arrays as elements when each yields multiple

0 comments on commit 1fb065c

Please sign in to comment.