Return to Snippet

Revision: 6307
at May 15, 2008 13:56 by vanne


Initial Code
# orginal article : http://blog.jayfields.com/2007/09/ruby-arraychunk.html

class Array
  
  # divides an array into a smaller chunks
  # [1,2,3,4].divide_by 2 # => [[1,2],[3,4]]
  def divide_by(number_of_chunks)
    chunks = (1..number_of_chunks).collect { [] }
    self.each_with_index do |item, index|
      chunks[index % number_of_chunks] << item
    end
    chunks
  end

  alias / divide_by

end

Initial URL


Initial Description


Initial Title
Array divide method

Initial Tags
textmate, ruby

Initial Language
Other