英文:
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的答案已经解释了为什么你当前的解决方案不起作用。我只想添加一个额外的选项。
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论