开始救援重试错误

时间:2016-11-22 07:46:50

标签: ruby loops if-statement

如果此代码执行else语句,我希望它从rescuebegin重试。 运行代码要求我输入,当我输入代码时,代码不起作用。

1-如何使此代码与运行retry语句一起使用?

2-为什么删除rescue会创建retry Syntax Error

# Creates input method
def input
  x = gets.to_i
end

#Begins the first choice
begin

  puts "What will you do?
  1- Get a closer look
  2- Go in the opposite direction
  Write your input an press enter:"
rescue
  #Calls input method
  choice = input
  if choice == 1
    puts "You get a closer look and..."
  elsif choice == 2
    puts "You go in the opposite direction, out of trouble"
  else 
    puts "Incorrect input, enter a number between the one's avaliables:"
  end

  #Retries if the choice is error
retry if choice != 1||2
end

1 个答案:

答案 0 :(得分:1)

使用救援块用于处理异常,我真的不认为这里需要它。循环将完成这项工作。

next