标签: arrays ruby
a = [1, 2, 3] b = [4, 5, 6]
如何在2D数组中组合两个数组?:
[[1, 4], [2, 5], [3, 6]]
答案 0 :(得分:30)
尝试Array#zip
a.zip(b) => [[1,4],[2,5],[3,6]]
答案 1 :(得分:10)
虽然zip显然是最直接的答案,但这也有效:
zip
[a, b].transpose => [[1, 4], [2, 5], [3, 6]]