问题
I'm learning programming through Ruby and I found the awesome Rubywarrior by Ryan Bates from Railscasts. Unfortunately I'm stuck in that my code is throwing up syntax error messages (unexpected $end).
I'm not asking for the answer, I would like to sort that out myself, but if someone could point out where my code is getting the error from that would be super. Thanks!
class Player
def initialize
@maxhealth = 20
@dying = 7
@previoushealth = @maxhealth
@health = warrior.health
@warrior = warrior
end
def play_turn(warrior)
# If there are no enemies, rest until health is 100%
turn_start_check(warrior)
actions(warrior)
turn_end_check(warrior)
end
def actions(warrior)
if @damaged_since_last_turn
warrior.shoot!
elsif
@health < @maxhealth
warrior.rest!
else
warrior.walk!
end
end
def hurt?(warrior)
warrior.health < 20
end
def healthy?(warrior)
warrior.health = 20
end
def alone?(warrior)
warrior.feel.empty?
end
def should_i_move?(warrior)
if healthy? and alone?
warrior.rest!
else
warrior.walk!
end
# Put code here for if health from previous turn is less than last term
# If true don't rest and keep moving forward
def turn_start_check(warrior)
@damaged_since_last_turn = @previoushealth > warrior.health
end
def turn_end_check(warrior)
@previoushealth = warrior.health
end
end
回答1:
My guess:
def should_i_move?(warrior)
if healthy? and alone?
warrior.rest!
else
warrior.walk!
end # <<MISSING THIS ONE
end
回答2:
That error message means that you are missing an end
keyword somewhere. Check your code to see if all your statements are properly written.
回答3:
With Ruby 1.9.3, if you turn on warnings, you get the following warnings:
-:46: warning: mismatched indentations at 'end' with 'if' at 42
-:57: warning: mismatched indentations at 'end' with 'def' at 41
and then the error
-:57: syntax error, unexpected $end, expecting keyword_end
line 46 corresponds to the first end
after def should_i_move?(warrior)
This should work with earlier versions of Ruby 1.9 as well.
来源:https://stackoverflow.com/questions/10586351/rubywarrior-level-4-cleaning-up-my-code-help