2015-10-20 75 views
-1

我对着我不断收到的错误消息猛击脑袋,读到“语法错误,意外的keyword_end,期待输入结束。”我无法为我的生活找到自己的错误。这可能是马虎,我是一个新手。并且任何有关防止未来这个特定问题的提示也将不胜感激!我在哪里错过了这个Ruby脚本中的“结束”?

$move_direction_hash = {"N" => [0,1], "E" => [1,0], "S" => [0,-1], "W" => [-1,0]} 
$cardinal_directions = ["N", "E", "S", "W"] 

class Martian 

attr_accessor :coordinate_x, :coordinate_y, :bearing, :direction_string 

def initialize (coordinate_x, coordinate_y, bearing, direction_string) 
    @coordinate_x = coordinate_x 
    @coordinate_y = coordinate_y 
    @bearing = bearing 
    @direction_string = direction_string 
end 


def check_valid 
    @coordinate_x.between?(0, $boundaries[0]) && coordinate_y.between?(0, $boundaries[1]) 
     return true 
    end 
end 


#will be second and last called in source code 
def get_final 
    return "#{coordinate_x} #{coordinate_y} #{bearing}" 
end  

def move_forward 
    # find where in the hash the bearing is 
    position_array = $move_direction_hash[@bearing] 
    # returns a temporary variable 
    # that is the 
    test_x = @coordinate_x + position_array[0] 
    test_y = @coordinate_y + position_array[1] 
    if $rovers_on_grid.include?([test_x.to_s, test_y.to_s]) 
     puts "Stopping Martian. You're about to crash!" 
     get_final 
     break 
    else 
     @coordinate_x = test_x 
     @coordinate_y = test_y 
     if check_valid == false 
      puts "Stopping Martian. About to run off the plateau." 
      get_final 
      break 
     else 
      return @coordinate_x, @coordinate_y 
     end 
    end 
end 

def add_to_grid 
$rovers_on_grid << [@x_coordinate, @y_coordinate] 
end 

def read_instructions  
    @direction_string.each_char do |direction| 
     if direction == "L" || direction == "R" 
      position = $cardinal_directions.index(@bearing) 
      if direction == "L" 
       position = (position - 1)%4 
       $cardinal_directions[position] 
      elsif direction == "R" 
       position = (position + 1)%4 
       $cardinal_directions[position] 
      else 
       puts "Error!" 
      end 
     @bearing = $cardinal_directions[position] 
     elsif direction == "M"  
      move_forward 
     end 
    end 
end 
end 
+0

当你报告一个错误信息时,你也应该给出错误发生的位置。这是不是精确定位'结局'? –

+0

@Cary,我将来会记住这一点。 TBH,我将在今后避免这样的无聊问题,但这不是一个很好的使用这个网站,虽然你们的规则。 –

回答

3

此错误位于check_valid方法中。你错过了如果。

def check_valid 
    if @coordinate_x.between?(0, $boundaries[0]) && coordinate_y.between?(0, $boundaries[1]) 
     return true 
    end 
end 

像steenslag提到如果不需要声明。你可以这样写:

def check_valid 
    return @coordinate_x.between?(0, $boundaries[0]) && coordinate_y.between?(0, $boundaries[1]) 
end 
+0

'@ coordinate_x.between?(0,$ boundaries [0])&& coordinate_y.between?(0,$ boundaries [1])'自身返回'true'或'false',所以'if'语句不是甚至有必要。 – steenslag

+0

@steenslag多数民众赞成在正确的,但比你不能使用结束关键字 – SnowMax

+0

不是意味着批评,更多的是作为一个附注(现在稍微改写)。 – steenslag