Sha256: dc63e15c2755096287eeb7ed3a610ca1e1aeed4dbee619cfbb3b593238eb4546

Contents?: true

Size: 893 Bytes

Versions: 3

Compression:

Stored size: 893 Bytes

Contents

module Enumerable

  unless method_defined?(:each_with_object)

    # A variation of #inject that saves one from having to
    # return the aggregate/memo argument.
    #
    # Say we want to count characters in a string. Using
    # the #each_with_object method we have:
    #
    #    string.each_with_object(Hash.new(0)) do |c, h|
    #      h[c] += 1
    #    end
    #
    # versus using #inject which would be:
    #
    #    string.inject(Hash.new(0)) do |h, c|
    #      h[c] +=1
    #      h
    #    end
    #
    # Notice that the order of the block parameters is reversed.
    #
    # This method used be called #injecting and had the same
    # parameter order as #inject, but Ruby 1.9 has adopted this
    # method, so we support it instead.

    def each_with_object(memo) #:yield:
      each do |element|
        yield(element, memo)
      end
      memo
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/enumerable/each_with_object.rb
facets-2.8.3 lib/core/facets/enumerable/each_with_object.rb
facets-2.8.2 lib/core/facets/enumerable/each_with_object.rb