I have an array:
arr=[[1,2,3],[4,5],[6]],
I have the following code:
arr.transpose
but it doesn\'t work,
A similar answer was posted (but deleted) an hour earlier:
arr = [[1, 2, 3], [4, 5], [6]]
arr[0].zip(*arr[1..-1])
#=> [[1, 4, 6], [2, 5, nil], [3, nil, nil]]
The above is equivalent to:
[1, 2, 3].zip([4, 5], [6])
This approach assumes that your first sub-array is always the longest. Otherwise the result will be truncated:
arr = [[1, 2], [3, 4, 5], [6]]
arr[0].zip(*arr[1..-1])
#=> [[1, 3, 6], [2, 4, nil]] missing: [nil, 5, nil]