Skip to content

Instantly share code, notes, and snippets.

@cohei
Created March 16, 2015 03:17
Show Gist options
  • Save cohei/c018f615389acb012ef7 to your computer and use it in GitHub Desktop.
Save cohei/c018f615389acb012ef7 to your computer and use it in GitHub Desktop.
def distribute(n, xs)
xs.each_slice(n).map { |array| l = array.length ; array + [nil]*(n-l) }.transpose.map(&:compact)
end
distribute(3, (1..7)) #=> [[1, 4, 7], [2, 5], [3, 6]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment