Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/magellan/extensions/array.rb

Instance Method Summary collapse

Instance Method Details

#chunk(max_size) ⇒ Object

Break down an array into chunks of a given max size. Example:

[1,2,3,4].chunk(3)    # => [[1,2,3],[4]]
[1,2,3,4].chunk(2)    # => [[1,2],[3,4]]


6
7
8
9
10
11
12
13
# File 'lib/magellan/extensions/array.rb', line 6

def chunk(max_size)
  result = []
  number_of_chunks = (self.size.to_f / max_size).ceil
  for i in 0...number_of_chunks do
    result << self[i*max_size...(i+1)*max_size]
  end
  result
end