最简单的方法是垂直合并这些数组

时间:2012-03-23 10:59:34

标签: ruby-on-rails ruby arrays

我有这些arras

names = ["Will","Bob","John","Ben"]
ages = [45,49,32,49]
postcodes = [9320,3991,1234,2993]

什么是堆叠它们的最有效方式,因此它们显示为

people = [["Will",45,9320],["Bob",49,3991],["John",32,1234],["Ben",49,2993]]

ruby​​是否有垂直合并这些功能的功能? (通过索引)没有所有那些讨厌的循环,简单又简单?

2 个答案:

答案 0 :(得分:8)

people = names.zip(ages, postcodes)

=> [["Will", 45, 9320], ["Bob", 49, 3991], ["John", 32, 1234], ["Ben", 49, 2993]]

答案 1 :(得分:0)

您可以使用Array#zip

people = names.zip ages, postcodes