Created
November 3, 2011 21:36
-
-
Save shamil614/1337849 to your computer and use it in GitHub Desktop.
Method to split up / divide an array into other arrays.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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