Sha256: 187f88991718ea26805a8cacf19f21557e883ad3bbf93d9e0e559228faa44d13

Contents?: true

Size: 1011 Bytes

Versions: 26

Compression:

Stored size: 1011 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]
  #
  def collect_with_index
    r = []
    each_index do |i|
      r << yield(self[i], i)
    end
    r
  end

  alias_method( :map_with_index, :collect_with_index )

  #--
  # Why the term counter? There may be a change in Ruby 2.0
  # to use this word instead of index. Index will
  # still be used for Array, since that is the proper meaning
  # in that context. In the mean time, aliases are provided.
  #++
  alias_method( :collect_with_counter, :collect_with_index )
  alias_method( :map_with_counter, :collect_with_index )

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCEnumerable < Test::Unit::TestCase

    def test_collect_with_index
      a = [1,2,3].collect_with_index{ |e,i| e*i }
      assert_equal( [0,2,6], a )
    end

  end

=end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/enumerable/collect_with_index.rb
facets-1.0.3 packages/core/lib/facet/enumerable/collect_with_index.rb
facets-1.0.0 lib/facet/enumerable/collect_with_index.rb
facets-1.2.1 lib/facets/core/enumerable/collect_with_index.rb
facets-1.1.0 lib/facet/enumerable/collect_with_index.rb
facets-1.2.0 lib/facets/core/enumerable/collect_with_index.rb
facets-1.3.0 lib/facets/core/enumerable/collect_with_index.rb
facets-1.3.1 lib/facets/core/enumerable/collect_with_index.rb
facets-1.3.2 lib/facets/core/enumerable/collect_with_index.rb
facets-1.3.3 lib/facets/core/enumerable/collect_with_index.rb
facets-1.4.0 lib/facets/core/enumerable/collect_with_index.rb
facets-1.4.1 lib/facets/core/enumerable/collect_with_index.rb
facets-1.4.2 lib/facets/core/enumerable/collect_with_index.rb
facets-1.4.3 lib/facets/core/enumerable/collect_with_index.rb
facets-1.4.4 lib/facets/core/enumerable/collect_with_index.rb
facets-1.4.5 lib/facets/core/enumerable/collect_with_index.rb
facets-1.7.30 lib/facets/core/enumerable/collect_with_index.rb
facets-1.7.0 lib/facets/core/enumerable/collect_with_index.rb
facets-1.7.38 lib/facets/core/enumerable/collect_with_index.rb
facets-1.7.46 lib/facets/core/enumerable/collect_with_index.rb