Created
January 12, 2010 09:43
-
-
Save rjungemann/275059 to your computer and use it in GitHub Desktop.
Split a Ruby array into regularly-sized chunks
This file contains 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 | |
def to_rows(column_length, padding = true) | |
i = 0 | |
array = [] | |
while i < self.size | |
chunk = self[i..(i + column_length - 1)] | |
chunk << [] while chunk.size < column_length if padding | |
array << chunk | |
i += column_length | |
end | |
array | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment