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