public
Fork of NZKoz/koz-rails
Description: Koz's rails git-svn clone
Homepage: http://www.rubyonrails.org
Clone URL: git://github.com/eventualbuddha/koz-rails.git
2-0-stable: Enumerable#group_by uses ActiveSupport::OrderedHash


git-svn-id: 
http://svn-commit.rubyonrails.org/rails/branches/2-0-stable@8946 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
nzkoz (author)
Thu Feb 28 20:28:51 -0800 2008
commit  a76085fdf6d29b498eed9478483fe9cf4155e63e
tree    c441b75d6e685a53df00448b0af5a6d6977687da
parent  f769bf68e13a32fa658d1da7e0bc4a6dafb53e51
...
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
 
31
32
33
34
35
36
37
...
15
16
17
 
 
 
 
 
 
 
 
 
 
 
 
 
18
19
20
21
 
 
22
23
24
0
@@ -15,23 +15,10 @@ module Enumerable
0
   # "2006-02-24 -> Transcript, Transcript"
0
   # "2006-02-23 -> Transcript"
0
   def group_by
0
- groups = []
0
-
0
- inject({}) do |grouped, element|
0
- index = yield(element)
0
-
0
- if group = grouped[index]
0
- group << element
0
- else
0
- group = [element]
0
- groups << [index, group]
0
- grouped[index] = group
0
- end
0
-
0
+ inject ActiveSupport::OrderedHash.new do |grouped, element|
0
+ (grouped[yield(element)] ||= []) << element
0
       grouped
0
     end
0
-
0
- groups
0
   end if RUBY_VERSION < '1.9'
0
 
0
   # Calculates a sum from the elements. Examples:
...
21
22
23
24
 
25
26
27
...
21
22
23
 
24
25
26
27
0
@@ -21,7 +21,7 @@ class EnumerableTests < Test::Unit::TestCase
0
       assert group.all? { |person| person.name == name }
0
     end
0
 
0
- assert_equal objects.uniq.map(&:name), grouped.map { |name, group| name }
0
+ assert_equal objects.uniq.map(&:name), grouped.keys
0
   end
0
 
0
   def test_sums

Comments

    No one has commented yet.