Using the Array::new
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