Skip to content

Instantly share code, notes, and snippets.

@ktheory
Forked from jimneath/gist:312432
Created February 24, 2010 20:23
Show Gist options
  • Save ktheory/313806 to your computer and use it in GitHub Desktop.
Save ktheory/313806 to your computer and use it in GitHub Desktop.
# I want to turn this:
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# into this:
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
def to_grid(ary)
array = ary.dup
row_length = Math.sqrt(array.size).to_i
result = []
while !array.empty?
result << array.shift(row_length)
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment