Skip to content

Instantly share code, notes, and snippets.

@solisoft
Created March 9, 2009 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solisoft/76402 to your computer and use it in GitHub Desktop.
Save solisoft/76402 to your computer and use it in GitHub Desktop.
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