I need a two-dimensional array in Ruby, which I can access, for example, as follows:
if @array[x][y] == "1" then @array[x][y] = "0"
The problem is this: I do not know the initial sizes of the sizes of the array, and I grow the array (with the << operator).
How to declare it as an instance variable, so I am not getting any errors?
undefined method `[]' for nil:NilClass (NoMethodError)
UPDATED QUESTION:
@array = Array.new {Array.new}
now works for me, so the comment from Matt below is correct!
I just figured out the reason I got the error, because I repeated this array as follows:
for i in 0..@array.length for j in 0..@array [0].length @array[i][j] ...
which, obviously, was wrong and caused an error. It should be like this:
for i in 0..@array.length-1 for j in 0..@array [0].length-1 @array[i][j] ...
kadrian
source share