1.9.3 and later can be created using Enumerator#with_index(offset=0).
Iterates the given block for each element with an index, which starts from offset. If no block is given, returns a new Enumerator that includes the index, starting from offset
[:a, :b, :c].map.with_index(2).to_a
#=> [[:a, 2], [:b, 3], [:c, 4]]
Enumerable#each_with_index(*args) { |obj, i| block }
Calls block with two arguments, the item and its index, for each item in enum. Given arguments are passed through to each().
arr.each_with_index.map { |x,i| [x, i+2] }
© 2024 OneMinuteCode. All rights reserved.