Skip to content

Commit 833d3e4

Browse files
committed
2-0-stable: Enumerable#group_by uses ActiveSupport::OrderedHash
git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/2-0-stable@8946 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
1 parent 6a386ea commit 833d3e4

File tree

2 files changed

+3
-16
lines changed

2 files changed

+3
-16
lines changed

activesupport/lib/active_support/core_ext/enumerable.rb

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,10 @@ module Enumerable
1515
# "2006-02-24 -> Transcript, Transcript"
1616
# "2006-02-23 -> Transcript"
1717
def group_by
18-
groups = []
19-
20-
inject({}) do |grouped, element|
21-
index = yield(element)
22-
23-
if group = grouped[index]
24-
group << element
25-
else
26-
group = [element]
27-
groups << [index, group]
28-
grouped[index] = group
29-
end
30-
18+
inject ActiveSupport::OrderedHash.new do |grouped, element|
19+
(grouped[yield(element)] ||= []) << element
3120
grouped
3221
end
33-
34-
groups
3522
end if RUBY_VERSION < '1.9'
3623

3724
# Calculates a sum from the elements. Examples:

activesupport/test/core_ext/enumerable_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_group_by
2121
assert group.all? { |person| person.name == name }
2222
end
2323

24-
assert_equal objects.uniq.map(&:name), grouped.map { |name, group| name }
24+
assert_equal objects.uniq.map(&:name), grouped.keys
2525
end
2626

2727
def test_sums

0 commit comments

Comments
 (0)