I have assumed that if the size of the array is not evenly divided by 4, the first so-many sliced arrays are to be of the same size and each have one more element than each each of remaining sliced arrays.
Here are a couple ways you could do that.
arr = [1,2,3,4,5,6,7,8,9]
Compute number of arrays having an extra element
def split_it(n, arr)
std_arr_size, nbr_bonus_arrays = arr.size.divmod(n)
nbr_bonus_elements = nbr_bonus_arrays * (std_arr_size + 1)
arr[0...nbr_bonus_elements].each_slice(std_arr_size+1).to_a +
arr[nbr_bonus_elements..-1].each_slice(std_arr_size).to_a
end
split_it(1, arr) #=> [[1, 2, 3, 4, 5, 6, 7, 8, 9]]
split_it(2, arr) #=> [[1, 2, 3, 4, 5], [6, 7, 8, 9]]
split_it(3, arr) #=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
split_it(4, arr) #=> [[1, 2, 3], [4, 5], [6, 7], [8, 9]]
split_it(5, arr) #=> [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
split_it(6, arr) #=> [[1, 2], [3, 4], [5, 6], [7], [8], [9]]
split_it(7, arr) #=> [[1, 2], [3, 4], [5], [6], [7], [8], [9]]
split_it(8, arr) #=> [[1, 2], [3], [4], [5], [6], [7], [8], [9]]
split_it(9, arr) #=> [[1], [2], [3], [4], [5], [6], [7], [8], [9]]
Use recursion
def split_it(n, arr)
return [arr] if n==1
m = (arr.size.to_f/n).ceil
[arr.first(m)] + split_it(n-1, arr[m..-1])
end
split_it(1, arr) #=> [[1, 2, 3, 4, 5, 6, 7, 8, 9]]
split_it(2, arr) #=> [[1, 2, 3, 4, 5], [6, 7, 8, 9]]
split_it(3, arr) #=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
split_it(4, arr) #=> [[1, 2, 3], [4, 5], [6, 7], [8, 9]]
split_it(5, arr) #=> [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
split_it(6, arr) #=> [[1, 2], [3, 4], [5, 6], [7], [8], [9]]
split_it(7, arr) #=> [[1, 2], [3, 4], [5], [6], [7], [8], [9]]
split_it(8, arr) #=> [[1, 2], [3], [4], [5], [6], [7], [8], [9]]
split_it(9, arr) #=> [[1], [2], [3], [4], [5], [6], [7], [8], [9]]