Iterate an array, n items at a time

后端 未结 2 1575
难免孤独
难免孤独 2021-02-04 23:35

I have an array:

[1,2,3,4,5,6,7,8,9,0] 

that I\'d like to iterate 3 at a time, which produces

1,2,3  and  4,5,6  and  7,8,9            


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-05 00:21

    Use .each_slice

    [1,2,3,4,5,6,7,8,9,0].each_slice(3) {|a| p a}
    

提交回复
热议问题