None of the answers work yet, so for now I came up with nest_concat! (deep_concat seemed wrong because we want only one deep):
class Array
def nest_concat! other
each_with_index { |arr, i| self[i].concat other[ i ] }
end
def nest_concat other
d = map( &:dup )
each_with_index { |arr, i| d[i].concat other[ i ] }
d
end
end
Allows you to write:
a = [ 'a', 'b' ]
b = [ 'c', 'd' ]
def c
return [ 'A', 'B' ], [ 'C', 'D' ]
end
[ a, b ].nest_concat! c
p a
p b
gives
["a", "b", "A", "B"]
["c", "d", "C", "D"]
source
share