Ruby undefined method `[]' for nil:NilClass (NoMethodError) -
I'm trying to make a version of Conway's life game through Ruby. I have created a grid class with @play_area as a playground, however, when I run my code, then @play_area becomes zero, because it has been evaluated twice before (when evaluated in line Then @play_area [x_mod] [y_mod] .alive). Why is this happening?
Edit Here is the initial function: is the function error In: ... The problem is with this line: @play_area [x] || = [] @ Y_length.times do | Y | @play_area [x] [y] = cell.new (x, y, false) and end
def initialize (sizex, sizey) @ X_length = sizex @ Y_length = sizey @play_area = [] # Preliminary Dead Cells @ x_length.times do | X | @play_area [x] || = [] @ Y_length.times do | Y | @play_area [x] [y] = cell New (X, Y, False) puts @play_area [x] [y]. End error ending
def Neighbor_name_f_gel (pos_x, pos_y) c = @play_area [pos_x] [pos_y] count = 0 ((pos_x-1) .. (pos_x + 1)). Do each. X | (Pos_y-1) .. (pos_y + 1)). Every do Y | Until @play_area [x] [y] .eql? (C) x_mod = x% (@x_length + 1) puts y_mod = y% (@__lamb + 1) x_mod puts y_mod if _play_area [x_mod] [y_mod] .alive count + 1 end end end end count Ends inspection of each cell in @play_area that shows that each cell is properly initialized, the inspection is produced here:
zone @ jon-macbook: ~ / program / ruby $ Rabbi main.rb # & lt; Cell: 0x00000000f919d8 @ live = false, @ alive_a = false, @x_pos = 0, @ y_pos = 0> # & Lt; Cell: 0x00000000f91848 @ alive = false, @ alive_next = false, @x_pos = 0, @ y_pos = 1 & gt; # & Lt; Cell: 0x00000000f916e0 @ alive = false, @ alive_next = false, @x_pos = 0, @ y_pos = 2 & gt; # & Lt; Cell: 0x00000000f915a0 @ alive = false, @ alive_next = false, @x_pos = 0, @ y_pos = 3 & gt; # & Lt; Cell: 0x00000000f91460 @ surviving = false, living_a = false, @x_pos = 0, @ y_pos = 4 & gt; # & Lt; Cell: 0x00000000f91320 @ alive = false, alive_a = false, @x_pos = 0, @ y_pos = 5> # & Lt; Cell: 0x00000000f911e0 @ alive = false, @ alive_next = false, @x_pos = 0, @ y_pos = 6> # & Lt; Cell: 0x00000000f910a0 @ alive = false, @ alive_next = false, @x_pos = 0, @ y_pos = 7> # & Lt; Cell: 0x00000000f90f38@ alive = false, @ alive_next = false, @x_pos = 0, @ y_pos = 8 & gt;
# & lt; Room: 0x00000000f1abf8 @ alive = false, @ alive_next = false, @x_pos = 19, @ y_pos = 7 & gt; # & Lt; Cell: 0x00000000f1aa90 @ alive = false, @ alive_next = false, @x_pos = 19, @ y_pos = 8 & gt; # & Lt; Cell: 0x00000000f1a900 @ alive = false, @ alive_next = false, @x_pos = 19, @ y_pos = 9 & gt; # & Lt; Cell: 0x00000000f1a798 @ alive = false, alive_a = false, @x_pos = 19, @ y_pos = 10> # & Lt; Cell: 0x00000000f1a658 @ surviving = false, living_a = false, @x_pos = 19, @ y_pos = 11 & gt; # & Lt; Cell: 0x00000000f1a518 @ alive = false, @ alive_next = false, @x_pos = 19, @ y_pos = 12> # & Lt; Cell: 0x00000000f1a3b0 @ alive = false, @ alive_next = false, @x_pos = 19, @ y_pos = 13 & gt; # & Lt; Cell: 0x00000000f1a270 @ alive = false, @ alive_next = false, @x_pos = 19, @ y_pos = 14 & gt; # & Lt; Cell: 0x00000000f1a130 @ alive = false, @ alive_next = false, @x_pos = 19, @ y_pos = 15 & gt; # & Lt; Cell: 0x00000000f19ff0 @ alive = false, @ alive_next = false, @x_pos = 19, @ y_pos = 16> # & Lt; Cell: 0x00000000f19e88 @ alive = false, @ alive_next = false, @x_pos = 19, @ y_pos = 17> # & Lt; Cell: 0x00000000f19d20 @ alive = false, @ alive_next = false, @x_pos = 19, @ y_pos = 18 & gt; # & Lt; Cell: 0x00000000f19be0 @ alive = false, @ alive_next = false, @x_pos = 19, @ y_pos = 1 9 & gt;
@play_area [x] [y] = cell.new (x, y, false)
@play_area [x] is zero you only have your multi-dimensional array One dimension has started.
Comments
Post a Comment