How to map with index in Ruby?

前端 未结 10 682
走了就别回头了
走了就别回头了 2020-12-07 06:59

What is the easiest way to convert

[x1, x2, x3, ... , xN]

to

[[x1, 2], [x2, 3], [x3, 4], ... , [xN, N+1]]
10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 07:37

    In ruby 1.9.3 there is a chainable method called with_index which can be chained to map.

    For example:

    array.map.with_index { |item, index| ... }
    

提交回复
热议问题