Skip to content

Instantly share code, notes, and snippets.

@shamil614
Created November 3, 2011 21:36
Show Gist options
  • Save shamil614/1337849 to your computer and use it in GitHub Desktop.
Save shamil614/1337849 to your computer and use it in GitHub Desktop.
Method to split up / divide an array into other arrays.
class Array
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
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment