I made two arrays, each with 1 million items:
a1 = 1_000_000.times.to_a
a2 = a1.clone
I tried to push a2 into a1:
a1.push *a2
This returns SystemStackError: stack level too deep
.
However, when I try with concat
, I don't get the error:
a1.concat a2
a1.length # => 2_000_000
I also don't get the error with the splat operator:
a3 = [*a1, *a2]
a3.length # => 2_000_000
Why is this the case? I looked at the documentation for Array#push
, and it is written in C. I suspect that it may be doing some recursion under the hood, and that's why it's causing this error for large arrays. Is this correct? Is it not a good idea to use push
for large arrays?