Skip to content

Instantly share code, notes, and snippets.

@theirix
Created June 17, 2011 15:09
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 theirix/1031609 to your computer and use it in GitHub Desktop.
Save theirix/1031609 to your computer and use it in GitHub Desktop.
Split array to chunks
def chunks a, size
chunk_count = a.size/size
chunk_count -= 1 if (a.size % size) == 0
(0..chunk_count).map do |k|
a.slice(k*size, size)
end
end
chunks((1...100).to_a, 20).each { |x| puts x.inspect }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment