Sha256: 8001d48109196453330ddfe89c1077da4a508e602a91bfc0936f5e54e49c9631

Contents?: true

Size: 821 Bytes

Versions: 58

Compression:

Stored size: 821 Bytes

Contents

# encoding: utf-8

module Enumerable

  if !Enumerable.instance_methods.include?('group_by')

    # Returns a hash, which keys are evaluated result from the block, and
    # values are arrays of elements in enum corresponding to the key. This
    # method is provided for backward compatibility with Ruby 1.8.6 and lower,
    # since {#group_by} is only available in 1.8.7 and higher.
    #
    # @yieldparam [Object] obj The object to classify
    #
    # @return [Hash]
    #
    # @example Grouping integers by rest by division through 3
    #
    #   (1..6).group_by { |i| i % 3 }
    #   # => { 0 => [3, 6], 1 => [1, 4], 2 => [2, 5] }
    def group_by
      groups = {}
      each do |item|
        key = yield(item)

        groups[key] ||= []
        groups[key] << item
      end
      groups
    end

  end

end

Version data entries

58 entries across 58 versions & 2 rubygems

Version Path
nanoc-3.7.3 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.7.2 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.7.1 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.7.0 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.6.11 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.6.10 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.6.9 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.6.8 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.6.7 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.6.6 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.6.5 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.6.4 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.6.3 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.6.2 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.6.1 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.6.0 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.5.0 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.5.0b2 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.5.0b1 lib/nanoc/extra/core_ext/enumerable.rb
nanoc-3.4.3 lib/nanoc/extra/core_ext/enumerable.rb