Skip to content

Instantly share code, notes, and snippets.

@rjungemann
Created January 12, 2010 09:43
Show Gist options
  • Save rjungemann/275059 to your computer and use it in GitHub Desktop.
Save rjungemann/275059 to your computer and use it in GitHub Desktop.
Split a Ruby array into regularly-sized chunks
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