Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/csquares/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#chunk(number_of_chunks) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/csquares/core_ext.rb', line 16

def chunk(number_of_chunks)
  chunks = Array.new(number_of_chunks) { [] }
  count = 0
  self.each do |e|
    chunks[count] << e 
    count = (count < number_of_chunks-1) ? count + 1 : 0
  end
  chunks
end

#chunk_into(max_size_of_array) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/csquares/core_ext.rb', line 26

def chunk_into(max_size_of_array)
  chunks = [[]]
  self.each do |e|
    chunks << [] unless chunks.last.length < max_size_of_array
    chunks.last << e
  end
  chunks
end