Skip to content
Permalink
Browse files

Merge pull request #575 from kostya/group_by

# enumerable#group_by
  • Loading branch information
alex committed Apr 7, 2013
2 parents 62d233b + a1c3a14 commit 1fb065cb4af83ffe447d07e7757c33dbda0642cb
Showing with 11 additions and 3 deletions.
  1. +11 −0 lib-topaz/enumerable.rb
  2. +0 −3 spec/tags/core/enumerable/group_by_tags.txt
@@ -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
@@ -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.
You can’t perform that action at this time.