如何将数组的第一个元素添加到另一个数组的第一个元素

时间:2015-04-28 05:49:28

标签: ruby arrays

我有两个阵列的波纹管[sic]:

address

我想打印以下输出:

first_name = ["prabhu" ,"raghu" , "satish"]
second_name = ["chaitanya", "varma", "venkey"]

4 个答案:

答案 0 :(得分:4)

您可以尝试以下操作:

{{1}}

答案 1 :(得分:3)

[first_name, second_name].transpose.map{|a| a.join(" ")}

答案 2 :(得分:2)

你也可以试试这个:

full_name = [];
first_name.each_with_index {|x, i|  x + second_name[i]}

答案 3 :(得分:0)

first_name.zip(second_name).map { |f, l| "#{f} #{l}" }
=> ["prabhu chaitanya", "raghu varma", "satish venkey"]

可替换地:

first_name.zip([" "].cycle, second_name).map(&:join)
=> ["prabhu chaitanya", "raghu varma", "satish venkey"]