如何切割数组值?

时间:2018-04-20 04:41:16

标签: arrays ruby

如何切割数组值?例如,我有:

a=[1,2,3,4,5,6,7,8,9,10]

我需要:

[[1, 2], [2, 3], [3, 4], [4, 5], ...]

1 个答案:

答案 0 :(得分:4)

a.each_cons(2).to_a
#=> [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10]]