Skip to content

Instantly share code, notes, and snippets.

@samstokes
Created November 8, 2010 12:29
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 samstokes/667650 to your computer and use it in GitHub Desktop.
Save samstokes/667650 to your computer and use it in GitHub Desktop.
Splits an enumerable into chunks. One-liner for use in Heroku console.
module Enumerable; def chunked(n); i=0; inject([]) {|chunks, item| if i % n == 0; chunks << [item]; else; chunks.last << item; end; i += 1; chunks }; end; end
@samstokes
Copy link
Author

D'oh: turns out Ruby already has Enumerable#each_slice which makes this redundant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment