Given:
a = [[1,"a"],[2,"b"]]
b = [[3,"c"],[4,"d"]]
I want to turn a
into [[1,"a"],[2,"b"][3,"c"],[4,"d"]]
. How can do this without +
? It creates a new array, which I want to avoid.
(a << b).flatten(1)
# => [1, "a", 2, "b", [3, "c"], [4, "d"]]
a.concat(b)
...............................
a = [[1,"a"],[2,"b"]]
b = [[3,"c"],[4,"d"]]
a[a.length, 0] = b
a
# > [[1, "a"], [2, "b"], [3, "c"], [4, "d"]]
concat
is the answer, but you could do this:
a.object_id #=> 70223889895340
a.replace(a+b) #=> [[1, "a"], [2, "b"], [3, "c"], [4, "d"]]
a #=> [[1, "a"], [2, "b"], [3, "c"], [4, "d"]]
a.object_id #=> 70223889895340
> b.inject(a, :<<)
#=> [[1, "a"], [2, "b"], [3, "c"], [4, "d"]]
What about?
a.push(b.shift) while b.any?
How about this?
a + b
=> [[1, "a"], [2, "b"], [3, "c"], [4, "d"]]