Sha256: 615c8c96de38177a9006b761d583fe0bf41f8a972318264ce9058c84d208d7d7

Contents?: true

Size: 372 Bytes

Versions: 6

Compression:

Stored size: 372 Bytes

Contents

module Enumerable

  # Same as #collect but with an iteration counter.
  #
  #   a = [1,2,3].collect_with_index { |e,i| e*i }
  #   a  #=> [0,2,6]
  #
  # CREDIT: Gavin Sinclair

  def map_with_index
    r = []
    each_index do |i|
      r << yield(self[i], i)
    end
    r
  end

  # Alias for map_with_index.

  alias_method :collect_with_index, :map_with_index

end

Version data entries

6 entries across 6 versions & 1 rubygems

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