For example, initialize the entire array to 0 or any other value.
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
For example, initialize the entire array to 0 or any other value.
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
Sure
Array.new(3) { Array.new(4, 0) }
=> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Array.new(3) { Array.new(4) { 0 } }. You need to understand that this answer is not the same as arr = Array.new(3, Array.new(4, 0)) #=> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]. The latter would be incorrect, for if we set arr[0][0] = 1 we would find arr #=> [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]. See Array::new.0 in an extra block is superfluous because integers are immutable. Ursus' answer is perfectly fine.