Module | DataMapper::Support::EnumerableExtensions |
In: |
lib/data_mapper/support/enumerable.rb
lib/data_mapper/support/enumerable.rb |
Group a collection of elements into groups within a Hash. The value returned by the block passed to group_by is the key, and the value is an Array of items matching that key.
names = %w{ sam scott amy robert betsy } names.group_by { |name| name.size } => { 3 => [ "sam", "amy" ], 5 => [ "scott", "betsy" ], 6 => [ "robert" ]}
# File lib/data_mapper/support/enumerable.rb, line 22 22: def group_by 23: inject(Hash.new { |h,k| h[k] = [] }) do |memo,item| 24: memo[yield(item)] << item; memo 25: end 26: end
Group a collection of elements into groups within a Hash. The value returned by the block passed to group_by is the key, and the value is an Array of items matching that key.
names = %w{ sam scott amy robert betsy } names.group_by { |name| name.size } => { 3 => [ "sam", "amy" ], 5 => [ "scott", "betsy" ], 6 => [ "robert" ]}
# File lib/data_mapper/support/enumerable.rb, line 22 22: def group_by 23: inject(Hash.new { |h,k| h[k] = [] }) do |memo,item| 24: memo[yield(item)] << item; memo 25: end 26: end