标签: arrays ruby
如何切割数组值?例如,我有:
a=[1,2,3,4,5,6,7,8,9,10]
我需要:
[[1, 2], [2, 3], [3, 4], [4, 5], ...]
答案 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]]