Sha256: c52e8b92cc3559d26bf99111b87b9d4b6c71502433724dd4103051fcccfbbebf

Contents?: true

Size: 582 Bytes

Versions: 7

Compression:

Stored size: 582 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
  #  CREDIT: 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 & 2 rubygems

Version Path
facets-2.4.0 lib/facets/enumerable/inject.rb
facets-2.4.1 lib/facets/enumerable/inject.rb
facets-2.4.2 lib/core/facets/enumerable/inject.rb
facets-2.4.3 lib/core/facets/enumerable/inject.rb
facets-2.4.4 lib/core/facets/enumerable/inject.rb
facets-2.4.5 lib/core/facets/enumerable/inject.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/enumerable/inject.rb