3

私はRubyを通じてプログラミングを学んでおり、RailscastsのRyanBatesによる素晴らしいRubywarriorを見つけました。残念ながら、コードが構文エラーメッセージ(予期しない$ end)をスローしていることに固執しています。

私は答えを求めているのではなく、自分でそれを整理したいと思いますが、誰かが私のコードがどこからエラーを受け取っているのかを指摘できれば、それは素晴らしいことです。ありがとう!

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
4

3 に答える 3

4

私の推測:

def should_i_move?(warrior)
  if healthy? and alone?
    warrior.rest!
  else
    warrior.walk!
  end  # <<MISSING THIS ONE
end
于 2012-05-14T15:30:46.090 に答える
4

このエラーメッセージは、endどこかにキーワードがないことを意味します。コードをチェックして、すべてのステートメントが正しく記述されているかどうかを確認してください。

于 2012-05-14T15:31:16.230 に答える
1

Ruby 1.9.3では、警告をオンにすると、次の警告が表示されます。

-:46: warning: mismatched indentations at 'end' with 'if' at 42
-:57: warning: mismatched indentations at 'end' with 'def' at 41

そしてエラー

-:57: syntax error, unexpected $end, expecting keyword_end

46行目はend後の最初の行に対応しますdef should_i_move?(warrior)

これは、以前のバージョンのRuby1.9でも機能するはずです。

于 2012-05-14T22:49:00.970 に答える