Using the [Array::new](<http://stackoverflow.com/documentation/ruby/253/arrays/908/create-array-with-arraynew#t=201701151514516164965>)
constructor, your can initialize an array with a given size and a new array in each of its slots. The inner arrays can also be given a size and and initial value.
For instance, to create a 3x4 array of zeros:
array = Array.new(3) { Array.new(4) { 0 } }
The array generated above looks like this when printed with p
:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
You can read or write to elements like this:
x = array[0][1]
array[2][3] = 2