重试 Ruby 中不正确的用户输入

huangapple go评论63阅读模式
英文:

Ruby retry incorrect user input

问题

我需要重试不正确的用户输入,但我的代码段不起作用

def parse_param
  begin 
    option = gets.chomp
    option = option.to_i
  rescue 
    retry if option > 6
  end

我在这里做错了什么?

英文:

I need to retry incorrect user input but my piece of code doesn't work

def parse_param
begin 
  option = gets.chomp
  option = option.to_i
rescue 
  retry if option > 6
end

What am I doing wrong here?

答案1

得分: 3

The rescue section will only be entered if an exception occurs in the begin block (see the docs for Exception Handling).

Since you don't raise any exception yourself and the remaining code doesn't raise one either, the rescue section is not executed.

You could manually raise an exception this way: (just for demonstration purposes)

def parse_param
  begin 
    option = gets.chomp
    option = option.to_i
    raise if option > 6
    option
  rescue 
    retry
  end
end

However, raising an exception just to rescue it right away is overly complicated. Also, an exception should be used for exceptional situations, i.e. something unexpected that your code cannot properly handle and which should be escalated to the caller.

For normal control flow, you could instead use a simple loop along with break, e.g.:

def parse_param
  loop do
    option = gets.chomp.to_i
    break option unless option > 6
  end
end

Ruby also has a redo statement which can be used to re-run the current iteration: (although it's rarely seen)

def parse_param
  loop do
    option = gets.chomp.to_i
    redo if option > 6
    break option
  end
end
英文:

> What am I doing wrong here?

The rescue section will only be entered if an exception occurs in the begin block (see the docs for Exception Handling).

Since you don't raise any exception yourself and the remaining code doesn't raise one either, the rescue section is not executed.

You could manually raise an exception this way: (just for demonstation purposes)

def parse_param
  begin 
    option = gets.chomp
    option = option.to_i
    raise if option > 6
    option
  rescue 
    retry
  end
end

However, raising an exception just to rescue it right-away is overly complicated. Also, an exception should be used for exceptional situations, i.e. something unexpected that your code cannot properly handle and which should be escalated to the caller.

For normal control flow, you could instead use a simple loop along with break, e.g.:

def parse_param
  loop do
    option = gets.chomp.to_i
    break option unless option > 6
  end
end

Ruby also has a redo statement which can be used to re-run the current iteration: (although it's rarely seen)

def parse_param
  loop do
    option = gets.chomp.to_i
    redo if option > 6
    break option
  end
end

答案2

得分: 2

1的答案已经解释了为什么你当前的解决方案不起作用。我只想添加一个额外的选项。

而不是使用loop循环,你可以使用whileuntil

def parse_param
  option = gets.chomp.to_i while !option || option > 6
  option
end

请注意,我们需要!option作为额外的检查,因为在第一次检查时,option的初始值是nil

英文:

The answer of Stefan already explains why your current solution does not work. I just wanted to add an additional option.

Instead of doing a loop with loop, you could use while or until.

def parse_param
  option = gets.chomp.to_i while !option || option > 6
  option
end

Note that we need !option as an additional check, since the initial value of option is nil on the first check.

huangapple
  • 本文由 发表于 2023年7月4日 21:49:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定