Reputation: 13
I have the following code. I am having problems getting at the game variables later on in my code, and upon further examination, my initialize code doesn't appear to be getting run. The debugger never fires. Why isn't my initialize method being run here?
class Game
attr_accessor :player, :status
def initialize
@player=Player.new
debugger
@status="active"
end
until @status=="finished"
turn=Turn.new
turn.start_turn
...MORE TURN CODE HERE...
end
end
Game.new
Upvotes: 1
Views: 118
Reputation: 160853
You just got an infinite loop with until @status=="finished"
, the @status
inside the class definition is nil
, so @status=="finished"
will never be true
.
Run the code below and you will see the infinite loop:
class Player; end
class Game
attr_accessor :player, :status
def initialize
@player=Player.new
@status="active"
end
until @status=="finished"
p 1
end
end
Upvotes: 0
Reputation: 22325
Unlike static languages like C++, Ruby actually evaluates the code inside class definitions. Ruby is getting caught in the until
block, so it never even finishes the class definition to get to the instantiation. You might want to put that code inside a method so that it only runs when you call it later on.
Upvotes: 1