Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 3727

Show
Ignore:
Timestamp:
03/01/06 21:12:18 (3 years ago)
Author:
marcel
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/core_ext/enumerable.rb

    r3726 r3727  
    2424  #   "2006-02-23 -> Transcript" 
    2525  def group_by 
    26     inject([]) do |groups, element| 
    27       value = yield(element) 
    28       if (last_group = groups.last) && last_group.first == value 
    29         last_group.last << element 
    30       else 
    31         groups << [value, [element]] 
    32       end 
     26    inject({}) do |groups, element| 
     27      (groups[yield(element)] ||= []) << element 
    3328      groups 
    3429    end 
    35   end 
     30  end  
    3631end