#products = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] limit = 4 l = products.length / limit + (products.length % limit == 0 ? 0 : 1) arr = [] 0.upto(limit - 1) do |i| arr << products.slice(i * l, limit) end