2016-11-04 58 views

回答

6

有。您可以结合使用Array#zipArray#flatten

b.zip(a).map(&:flatten) 
#=> [["a", "b", "x"], ["c", "d", "y"], ["e", "f", "z"]] 
3

另一种方式是:

[b, a].transpose.map(&:flatten) 
#=> [["a", "b", "x"], ["c", "d", "y"], ["e", "f", "z"]] 

:)

2

这里是这样做另一种方式:

a = ["x","y","z"] 
b = [["a","b"],["c","d"],["e","f"]] 

b.map.with_index {|arr, idx| arr << a[idx]} 
#=> [["a", "b", "x"], ["c", "d", "y"], ["e", "f", "z"]] 
1
enum = a.to_enum 
b.map { |arr| arr << enum.next } 
    #=> [["a", "b", "x"], ["c", "d", "y"], ["e", "f", "z"]] 
相关问题