Created
November 8, 2010 12:29
-
-
Save samstokes/667650 to your computer and use it in GitHub Desktop.
Splits an enumerable into chunks. One-liner for use in Heroku console.
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
D'oh: turns out Ruby already has Enumerable#each_slice which makes this redundant.