How to combine multiple arrays of the same size in ruby

后端 未结 2 1884
余生分开走
余生分开走 2021-01-19 11:31

If I have 3 or more arrays I want to combine into one, how do I do that in ruby? Would it be a variation on zip?

For example, I have

a = [1, 2, 3] b =

相关标签:
2条回答
  • 2021-01-19 11:41

    I would use Array#zip as below:

    a = [1, 2, 3]
    b = [4, 5, 6]
    c = [7, 8, 9]
    a.zip(b, c)
    #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
    
    0 讨论(0)
  • 2021-01-19 11:59
    [a,b,c].transpose
    

    is all you need. I prefer this to zip 50% of the time.

    0 讨论(0)
提交回复
热议问题