- # 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
Array divide method
Posted by Anonymous on May Wed 16th 8:58 AM - Never Expires| View : 17Syntax: TEXT
403 characters | 18 lines | 403 Bytes
