Class: Array

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

Instance Method Summary collapse

Instance Method Details

#chunk(pieces = 2) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/peplum/core_ext/array.rb', line 3

def chunk( pieces = 2 )
  return self if pieces <= 0

  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