Reputation: 99
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"]]
Upvotes: 2
Views: 1759
Reputation: 10251
> b.inject(a, :<<)
#=> [[1, "a"], [2, "b"], [3, "c"], [4, "d"]]
Upvotes: 2
Reputation: 110725
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
Upvotes: 1
Reputation: 34338
a = [[1,"a"],[2,"b"]]
b = [[3,"c"],[4,"d"]]
a[a.length, 0] = b
a
# > [[1, "a"], [2, "b"], [3, "c"], [4, "d"]]
Upvotes: 1