I defined a node class and player class as following:
class Node < OpenStruct
def initialize(parent,tag,&block)
super()
self.parent = parent
self.parent.children << self unless parent.nil?
self.children = []
self.tag = tag
instance_eval(&block) unless block.nil?
end
end
class Player < Node
def initialize(parent)
Node.new(parent,:player) do
self.turn_num = 1
end
end
end
The instance variable player was created by
player = Player.new(room) # room is the parent node which was defined
puts player.turn_num
And I got the error:
in `method_missing': undefined method `[]' for nil:NilClass (NoMethodError)
Could you help me figure out where went wrong? Thanks!
Edit:
The problem should be the initialize in the Player class. I changed my codes
class Player < Node
def self.new(parent)
Node.new(parent,:player) do
self.turn_num = 1
end
end
end
Then there is no error.What's wrong with the initialize here?
niland you are trying to do stuff with it.method_missing': undefined method[]' for nil:NilClass (NoMethodError) from test.rb:6:in `<main>' (referring to the line 'puts player.turn_num')