diff options
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 17 | ||||
-rw-r--r-- | activesupport/test/core_ext/enumerable_test.rb | 2 |
2 files changed, 3 insertions, 16 deletions
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 35a8e0538c..c448d0454d 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -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: diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index 234dc98de8..9567f22e9a 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -21,7 +21,7 @@ class EnumerableTests < Test::Unit::TestCase 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 |