I have an array:
arr=[[1,2,3],[4,5],[6]],
I have the following code:
arr.transpose
but it doesn\'t work,
Using zip as in Stefan's answer is the most straightforward, but if you insist on using transpose, then:
l = arr.map(&:length).max
arr.map{|e| e.values_at(0...l)}.transpose
# => [[1, 4, 6], [2, 5, nil], [3, nil, nil]]
Or without using either:
Array.new(arr.map(&:length).max){|i| arr.map{|e| e[i]}}
# => [[1, 4, 6], [2, 5, nil], [3, nil, nil]]