From 0d92ce59ff45a0e2091c89e5ab976bff803f4b22 Mon Sep 17 00:00:00 2001 From: Marcel Molina Date: Wed, 1 Mar 2006 21:12:18 +0000 Subject: [PATCH] Make Enumerable#group_by return a Hash (sacrificing the preservation of ordering) so that it is more compatible with the version that is in Ruby 1.9 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3727 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/core_ext/enumerable.rb | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 8a097dcd90..a49c4c701b 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -23,14 +23,9 @@ module Enumerable #:nodoc: # "2006-02-24 -> Transcript, Transcript" # "2006-02-23 -> Transcript" def group_by - 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 + inject({}) do |groups, element| + (groups[yield(element)] ||= []) << element groups end - end + end end