Permalink
Browse files

Enumerable#group_by uses ActiveSupport::OrderedHash

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8700 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent fc71a07 commit 8273280a637a5e40de2c37dd1da507c749b11aab @jeremy jeremy committed Jan 23, 2008
@@ -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:
@@ -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

0 comments on commit 8273280

Please sign in to comment.