Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby undefined method `[]' for nil:NilClass (NoMethodError)

Tags:

ruby

I'm trying to make a version of Conway's game of life using ruby. I've created a Grid class with @play_area as an instance variable. However, when I run my code, @play_area turns up nil after it had already been evaluated twice (when evaluated in the line if @play_area[x_mod][y_mod].alive ). Why is this happening?

EDIT

Here's the initialize function:

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

Here's the function the error occurs in:

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

The inspect on each of the cells in @play_area shows that each one of the cells is initialized correctly, here is the output of the 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>

...

#<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>
like image 489
steamingramen Avatar asked Feb 17 '14 05:02

steamingramen


People also ask

What does undefined method for nil NilClass mean?

The Undefined method for nil:NILClass occurs when you attempt to use a formula on a blank datapill. This indicates that the datapill was not provided any value at runtime.

What is an error NoMethodError undefined method for nil NilClass?

Explained. This is a common Ruby error which indicates that the method or attribute for an object you are trying to call on an object has not been defined. For example, the String class in Ruby has the method size (which is synonymous with length , so I can write...

What is NoMethodError in Ruby?

Raised when a method is called on a receiver which doesn't have it defined and also fails to respond with method_missing .

What does undefined method mean?

The undefined method is also called the NoMethodError exception, and it's the most common error within projects, according to The 2022 Airbrake Error Data Report. It occurs when a receiver (an object) receives a method that does not exist.


1 Answers

The problem is with this line:

@play_area[x][y] = Cell.new(x, y, false)

@play_area[x] is nil. You've only initialized one dimension of your multidimensional array. You need to initialize each element of @play_area[x] to an array before trying to add an element to it.

@x_length.times do |x|
  @play_area[x] ||= []
  @y_length.times do |y|
    @play_area[x][y] = Cell.new(x, y, false)
  end
end
like image 84
meagar Avatar answered Sep 26 '22 08:09

meagar