Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/options-lib/helpers.rb
Instance Method Summary collapse
-
#chunk(pieces = 2) ⇒ Object
Break the array into smaller arrays (chuncks) If the lenght of the array is not divisible by the number of chunks (pieces) then first chunk will accomodate the extra item and be the larger chunk Ex: [1,2,3,4].chunck # => [[1,2], [3,4]].
Instance Method Details
#chunk(pieces = 2) ⇒ Object
Break the array into smaller arrays (chuncks) If the lenght of the array is not divisible by the number of chunks (pieces) then first chunk will accomodate the extra item and be the larger chunk Ex: [1,2,3,4].chunck # => [[1,2], [3,4]]
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/options-lib/helpers.rb', line 23 def chunk(pieces=2) len = self.length; mid = (len / pieces) chunks = [] start = 0 1.upto(pieces) do |i| last = start + mid last = last - 1 unless len % pieces >= i chunks << self[start..last] || [] start = last + 1 end chunks end |