У меня есть это движение:
def split_array(array,size)
index = 0
results = []
if size > 0
while index <= array.size
res = array[index,size]
results << res if res.size != 0
index += size
end
end
return results
end
Если я работаю на нем [1,2,3,4,5,6]
как split_array([1,2,3,4,5,6],3)
это произведет этот массив:
[[1,2,3],[4,5,6]]
. Там что-то уже доступно, который может сделать это в Ruby 1.8.7?
[1,2,3,4,5,6].each_slice(3).to_a
#=> [[1, 2, 3], [4, 5, 6]]
Для 1.8,6:
require 'enumerator'
[1,2,3,4,5,6].enum_for(:each_slice, 3).to_a