我有这些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是否有垂直合并这些功能的功能? (通过索引)没有所有那些讨厌的循环,简单又简单?
答案 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