diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2007-12-31 21:30:17 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2007-12-31 21:30:17 +0000 |
commit | 438b108c6bea3d9781c0ecd5e53d362b29fe7f6b (patch) | |
tree | b56970f7b70ef78e812702d2a3d1404f072c2b0c | |
parent | 07de7612bd11f152df4bb9e68f63de61ec0e6c3d (diff) | |
download | rails-438b108c6bea3d9781c0ecd5e53d362b29fe7f6b.tar.gz rails-438b108c6bea3d9781c0ecd5e53d362b29fe7f6b.tar.bz2 rails-438b108c6bea3d9781c0ecd5e53d362b29fe7f6b.zip |
Changed the implementation of Enumerable#group_by to use a double array approach instead of a hash such that the insert order is honored [DHH/Marcel]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8516 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 9 |
2 files changed, 9 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 81eeb92e32..059a3ebbe0 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Changed the implementation of Enumerable#group_by to use a double array approach instead of a hash such that the insert order is honored [DHH/Marcel] + * remove multiple enumerations from ActiveSupport::JSON#convert_json_to_yaml when dealing with date/time values. [rick] * Hash#symbolize_keys skips keys that can't be symbolized. #10500 [Brad Greenlee] diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index f35c8f86c5..c3a351538f 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -15,8 +15,13 @@ module Enumerable # "2006-02-24 -> Transcript, Transcript" # "2006-02-23 -> Transcript" def group_by - inject({}) do |groups, element| - (groups[yield(element)] ||= []) << element + inject([]) do |groups, element| + value = yield(element) + if (last_group = groups.last) && last_group.first == value + last_group.last << element + else + groups << [value, [element]] + end groups end end if RUBY_VERSION < '1.9' |