Creating matrix with `Array.new(n, Array.new)`

Follow the code:

@gameboard = Array.new(3, Array.new(3, " "))
@gameboard.map { |a| a.object_id }
# => [76584030, 76584030, 76584030]

means new(size=0, obj=nil) method creates an array of size, having the same ob.

But new(size) {|index| block } method works in a different way; it creates an array of size, having different obs.

See the code below:

@gameboard = Array.new(3) { Array.new(3, " ") }
@gameboard.map { |a| a.object_id }
# => [75510080, 75509920, 75509540]

The above is the same as your second code example:

@gameboard = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]
@gameboard.map { |a| a.object_id }
# => [80194090, 80193400, 80193080]

If you change or update the the value at index 1 of the first element array of @gameboard, it wouldn’t affect all other inner array elements.

@gameboard = Array.new(3) { Array.new(3, " ") }
@gameboard[0][1] = 2
@gameboard
# => [[" ", 2, " "], [" ", " ", " "], [" ", " ", " "]]

Leave a Comment