Skip to content

Commit

Permalink
Enumerable#group_by uses ActiveSupport::OrderedHash
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8700 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Jan 23, 2008
1 parent fc71a07 commit 8273280
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 16 deletions.
17 changes: 2 additions & 15 deletions activesupport/lib/active_support/core_ext/enumerable.rb
Expand Up @@ -15,23 +15,10 @@ module Enumerable
# "2006-02-24 -> Transcript, Transcript"
# "2006-02-23 -> Transcript"
def group_by
groups = []

inject({}) do |grouped, element|
index = yield(element)

if group = grouped[index]
group << element
else
group = [element]
groups << [index, group]
grouped[index] = group
end

inject ActiveSupport::OrderedHash.new do |grouped, element|
(grouped[yield(element)] ||= []) << element
grouped
end

groups
end if RUBY_VERSION < '1.9'

# Calculates a sum from the elements. Examples:
Expand Down
2 changes: 1 addition & 1 deletion activesupport/test/core_ext/enumerable_test.rb
Expand Up @@ -21,7 +21,7 @@ def test_group_by
assert group.all? { |person| person.name == name }
end

assert_equal objects.uniq.map(&:name), grouped.map { |name, group| name }
assert_equal objects.uniq.map(&:name), grouped.keys
end

def test_sums
Expand Down

0 comments on commit 8273280

Please sign in to comment.