transpose.rb
# Array#transpose
a = [[1,2,3],[4,5,6],[7,8,9]]
a.transpose
#=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
# same return
a[0].zip(*a[1..-1])
#=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Go to list of users who liked
More than 5 years have passed since last update.
# Array#transpose
a = [[1,2,3],[4,5,6],[7,8,9]]
a.transpose
#=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
# same return
a[0].zip(*a[1..-1])
#=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Register as a new user and use Qiita more conveniently
Go to list of users who liked