用于nil的Ruby未定义方法`[]':NilClass(NoMethodError)

ste*_*men 18 ruby

我正在尝试使用红宝石制作Conway的生活游戏版本.我用@play_area创建了一个Grid类作为实例变量.但是,当我运行我的代码时,@ play_area在已经被评估两次之后变为零(如果在@play_area [x_mod] [y_mod] .alive中在行中进行评估).为什么会这样?

编辑

这是初始化函数:

def initialize(sizex, sizey)
    @x_length = sizex
    @y_length = sizey
    @play_area = []
    #initialize 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].inspect
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

这是错误发生在的函数:

def neighbor_num_of_cell(pos_x,pos_y)
    c = @play_area[pos_x][pos_y]
    count = 0
    ((pos_x-1)..(pos_x+1)).each do |x|
        ((pos_y-1)..(pos_y+1)).each do |y|
            unless @play_area[x][y].eql?(c)
                x_mod = x % (@x_length + 1)
                y_mod = y % (@y_length + 1)
                puts x_mod
                puts y_mod
                if @play_area[x_mod][y_mod].alive
                    count += 1
                end
            end
        end
    end
    count
end
Run Code Online (Sandbox Code Playgroud)

对@play_area中每个单元格的检查显示每个单元格都已正确初始化,这里是inspect的输出:

jon@jon-MacBook:~/programs/ruby$ ruby main.rb 
#<Cell:0x00000000f919d8 @alive=false, @alive_next=false, @x_pos=0, @y_pos=0>
#<Cell:0x00000000f91848 @alive=false, @alive_next=false, @x_pos=0, @y_pos=1>
#<Cell:0x00000000f916e0 @alive=false, @alive_next=false, @x_pos=0, @y_pos=2>
#<Cell:0x00000000f915a0 @alive=false, @alive_next=false, @x_pos=0, @y_pos=3>
#<Cell:0x00000000f91460 @alive=false, @alive_next=false, @x_pos=0, @y_pos=4>
#<Cell:0x00000000f91320 @alive=false, @alive_next=false, @x_pos=0, @y_pos=5>
#<Cell:0x00000000f911e0 @alive=false, @alive_next=false, @x_pos=0, @y_pos=6>
#<Cell:0x00000000f910a0 @alive=false, @alive_next=false, @x_pos=0, @y_pos=7>
#<Cell:0x00000000f90f38 @alive=false, @alive_next=false, @x_pos=0, @y_pos=8>
Run Code Online (Sandbox Code Playgroud)

...

#<Cell:0x00000000f1abf8 @alive=false, @alive_next=false, @x_pos=19, @y_pos=7>
#<Cell:0x00000000f1aa90 @alive=false, @alive_next=false, @x_pos=19, @y_pos=8>
#<Cell:0x00000000f1a900 @alive=false, @alive_next=false, @x_pos=19, @y_pos=9>
#<Cell:0x00000000f1a798 @alive=false, @alive_next=false, @x_pos=19, @y_pos=10>
#<Cell:0x00000000f1a658 @alive=false, @alive_next=false, @x_pos=19, @y_pos=11>
#<Cell:0x00000000f1a518 @alive=false, @alive_next=false, @x_pos=19, @y_pos=12>
#<Cell:0x00000000f1a3b0 @alive=false, @alive_next=false, @x_pos=19, @y_pos=13>
#<Cell:0x00000000f1a270 @alive=false, @alive_next=false, @x_pos=19, @y_pos=14>
#<Cell:0x00000000f1a130 @alive=false, @alive_next=false, @x_pos=19, @y_pos=15>
#<Cell:0x00000000f19ff0 @alive=false, @alive_next=false, @x_pos=19, @y_pos=16>
#<Cell:0x00000000f19e88 @alive=false, @alive_next=false, @x_pos=19, @y_pos=17>
#<Cell:0x00000000f19d20 @alive=false, @alive_next=false, @x_pos=19, @y_pos=18>
#<Cell:0x00000000f19be0 @alive=false, @alive_next=false, @x_pos=19, @y_pos=19>
Run Code Online (Sandbox Code Playgroud)

mea*_*gar 4

问题出在这一行:

@play_area[x][y] = Cell.new(x, y, false)
Run Code Online (Sandbox Code Playgroud)

@play_area[x]为零。您仅初始化了多维数组的一维。@play_area[x]在尝试向数组添加元素之前,您需要先将 的每个元素初始化为数组。

@x_length.times do |x|
  @play_area[x] ||= []
  @y_length.times do |y|
    @play_area[x][y] = Cell.new(x, y, false)
  end
end
Run Code Online (Sandbox Code Playgroud)