Created
March 9, 2009 18:06
-
-
Save solisoft/76402 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# Split an array with defined size | |
# [1,2,3,4,5,6,7].split_by_size(2) #=> [[1, 2], [3,4], [5,6], [7]] | |
def split_by_size(size = 500) | |
tmp = [] | |
newarray = [] | |
self.each_with_index do |a, i| | |
if i % size == 0 | |
unless tmp.blank? | |
newarray.push tmp + [a] | |
tmp = [] | |
end | |
else | |
tmp.push a | |
end | |
end | |
newarray + [tmp] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment