Skip to content

Instantly share code, notes, and snippets.

@TorD
Last active August 29, 2015 14:21
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 TorD/08f971dceebc85580108 to your computer and use it in GitHub Desktop.
Save TorD/08f971dceebc85580108 to your computer and use it in GitHub Desktop.
# Efficiently make a grid coordinate out of a flat integer size, with the limit set to 2 per row
# Change occurence of 2 in method to change limit per row
def grid_size(size)
Array.new(size){|n| [n % 2, n / 2]}
end
# Example return
grid_size(6) => [[0, 0], [1, 0], [0, 1], [1, 1], [0, 2], [1, 2]]
# Or in more readable return format
[[0, 0], [1, 0],
[0, 1], [1, 1],
[0, 2], [1, 2]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment