是否可以在“if”中使用“retry”关键字?

ddn*_*mad 2 ruby if-statement inline

我必须用begin- rescue-end块包围所有东西。我写的代码看起来像:

begin
  bet = Dialogs.enter_your_bet
  approx.calculate_average_profit(bet)
  approx.print_profits_table
  retry if Dialogs.play_again?
rescue;retry
end
Run Code Online (Sandbox Code Playgroud)

该行retry if Dialogs.play_again?导致以下错误:

./main:14: Invalid retry
./main: compile error (SyntaxError)
Run Code Online (Sandbox Code Playgroud)

是否可以在没有常规多行方法的情况下使这种内联与retry子句一起if使用?ifend

spi*_*ann 5

retryrescue在块(或迭代器)中工作。它适用于if. 尝试这个:

begin
  bet = Dialogs.enter_your_bet
  approx.calculate_average_profit(bet)
  approx.print_profits_table
rescue
  retry if Dialogs.play_again?
end
Run Code Online (Sandbox Code Playgroud)