2016-11-04 1 views

Antwort

6

Es gibt. Sie können Array#zip in Verbindung mit Array#flatten verwenden:

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

einen anderen Weg:

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

:)

2

Hier ist eine weitere Möglichkeit, dies zu tun:

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"]] 
Verwandte Themen