All the proposed method to get this array [[], [], [],...] works fine:
Array.new(n, []) # the best, 20 times faster
Array.new(n){ [] }
n.times.map { [] }
The first is the fastest, so the best, but works strangely (see next).
it created an array with all the sub arrays being the same object
If i get the point, you mean that whit the methods described happens what follows:
a = Array.new(5, [])
p a # => [[], [], [], [], [], [], [], [], [], []]
p a.map { |e| e.object_id} # => [70189488740180, 70189488740180, 70189488740180, 70189488740180, 70189488740180]]
The object is the same, so if you try to fill subarrays with values, all of the subarrays assume the same value (replication):
a[0][0] = 10
p a # => [[10], [10], [10], [10], [10]]
To avoid this don't pass a default value, but map to empty array instead:
a = Array.new(5).map{ |e| [] }
p a # => [[], [], [], [], []]
Or pass the block
a = Array.new(5){ [] }
a = 5.times.map { [] }
Now each subarray is an independent object:
p a.map { |e| e.object_id} # => [70253023825640, 70253023825620, 70253023825600, 70253023825580, 70253023825560]
And if you insert some values there is no replication:
a[0][0] = 10
a[1][0] = 20
p a # => [[10], [20], [], [], []]
n.times.map { |_| [] }Array.new(n){[]}Matrix?