Ruby array each_slice_with_index?

前端 未结 3 1719
野趣味
野趣味 2021-02-01 13:30

If I have arr = [1, 2, 3, 4] I know I can do the following...

> arr.each_slice(2) { |a, b| puts \"#{a}, #{b}\" }
1, 2
3, 4

...A

3条回答
  •  甜味超标
    2021-02-01 13:46

    arr.each_slice(2).with_index { |(*a), i| ...
    

    also note that the array, first parameter of the block, can be *arr

提交回复
热议问题