<分区>
Possible Duplicate:
How to split (chunk) a Ruby array into parts of X elements?
我想将一个数组拆分为一个子数组数组。
例如,
big_array = (0...6).to_a
我们如何将这个大数组切割成数组数组(最大长度为 2 项),例如:
arrays = big_array.split_please(2)
哪里...
arrays # => [ [0, 1],
[2, 3],
[4, 5] ]
注意: 我问这个问题,因为为了做到这一点,我目前正在这样编码:
arrays = [
big_array[0..1],
big_array[2..3],
big_array[4..5]
]
...这太丑了。当 big_array.length > 100
时,代码非常难以维护。