Skip to content

Commit

Permalink
Changed the implementation of Enumerable#group_by to use a double arr…
Browse files Browse the repository at this point in the history
…ay 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
  • Loading branch information
dhh committed Dec 31, 2007
1 parent 07de761 commit 438b108
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
9 changes: 7 additions & 2 deletions activesupport/lib/active_support/core_ext/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 438b108

Please sign in to comment.