Sha256: 811ccc238e81dbf8943554bbf53bd627e19e57afecca0aad6c1891c3e5d44f97

Contents?: true

Size: 568 Bytes

Versions: 7

Compression:

Stored size: 568 Bytes

Contents

module Enumerable

  # A small variation of #Inject that save one from having to
  # return the aggregating or memo argument.
  #
  # Say you want to count letters.
  #
  #    some_text.inject!(Hash.new(0)) {|h,l| h[l] += 1}
  #
  # vs
  #
  #    some_text.inject(Hash.new(0)) {|h,l| h[l] +=1; h}
  #
  # CREDIT: David Black, Louis J Scoras

  def inject!(s)
    k = s
    each { |i| yield(k, i) }
    k
  end

  # HERE AS BACKUP, TO ENSURE THE ABOVE HAS THE SAME FUNCTIONALITY.
  #def injecting(s)
  #  inject(s) do |k, i|
  #    yield(k, i); k
  #  end
  #end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
facets-2.8.1 lib/core/facets/enumerable/inject.rb
facets-2.8.0 lib/core/facets/enumerable/inject.rb
facets-2.7.0 lib/core/facets/enumerable/inject.rb
facets-2.6.0 lib/core/facets/enumerable/inject.rb
facets-2.5.0 lib/core/facets/enumerable/inject.rb
facets-2.5.1 lib/core/facets/enumerable/inject.rb
facets-2.5.2 lib/core/facets/enumerable/inject.rb