Logan Capaldo wrote:
> % cat enumerator_vs_facets.rb
> #!/usr/bin/env ruby
> require 'enumerator'
> require 'benchmark'
>
> class Array
>    def facets_each_slice(n=nil, &yld)
>      n = yld.arity.abs unless n
>      i=0
>      while i < self.length
>        yld.call(*self.slice(i,n))
>        i+=n
>      end
>    end
> end
>
> arrays = [ (1..97).to_a, (0..99).to_a, ["hello", "world"] ]
> N = 1000
> Benchmark.bmbm do |bm|
>    bm.report("Facets: ") do
>      N.times do
>        arrays.each do |array|
>          array.facets_each_slice(3) { |*x| "#{x}" }
>        end
>      end
>    end
>
>    bm.report("Enumerator: ") do
>      N.times do
>        arrays.each do |array|
>          array.each_slice(3) { |*x| "#{x}" }
>        end
>      end
>    end
> end
>
>
>
> % ruby enumerator_vs_facets.rb
> Rehearsal ------------------------------------------------
> Facets:        1.250000   0.010000   1.260000 (  1.364671)
> Enumerator:    1.380000   0.010000   1.390000 (  1.442132)
> --------------------------------------- total: 2.650000sec
>
>                     user     system      total        real
> Facets:        1.250000   0.010000   1.260000 (  1.315879)
> Enumerator:    1.390000   0.010000   1.400000 (  1.447592)

I believe the OP asked how to split an array in "x" sub arrays of equal
length.
To me, it appears that the response show how to partition an array into
sub arrays of "y" elements.

The following method does what the OP asked for, as far as I read it.

class Array
	def split(n, b=[], d=[])
		#########################################################################
		# evenly split array 'a' into 'n' sub arrays
		#########################################################################
		if self.size == 0 or n <= 0
    	b = nil
		elsif n == 1
    	b = *self
		else
			# determine how many elements of the array should go in each sub
arrays
			buckets = n
			length = self.size
			while buckets > 0
				elements = length / buckets + (length % buckets > 0 ? 1 : 0)
				d << elements
				length -= elements
				buckets -= 1
			end
			# p d

			# evenly distribute array elements into an array with 'n' sub arrays
			start = 0  # start
			0.upto(n-1) do |idx|
				len = d[idx]
				b[idx] = *self.slice(start,len)
				start += len
			end
		end
		b
	end
end