【发布时间】:2019-05-21 18:10:25
【问题描述】:
我总是像这样使用提前返回来减少嵌套 if(使用其他语言编程):
//instead of nested if like this
if (condition1) {
if (condition2) {
if (condition3) {
//... finally meet all conditions
//do the job
}
}
}
//return early
if (!condition1) {
//clean up resource in step #1
return
}
if (!condition2) {
//clean up resource in step #2
return
}
if (!condition3) {
//clean up resource in step #2
return
}
...
//finally meet all conditions
但是我如何在 ruby 中提前返回?我无法在 Ruby 的 if 块内返回。我得到了错误
“未捕获的异常:意外返回 ... `block (2 levels) in ': 意外返回 (LocalJumpError)”
----更新-----
对不起,我忘了说,在这种简单的情况下它可以工作
def early(input)
if (input <0)
puts 'should >0'
return
end
puts 'good'
end
我正在学习 Fiber,我使用来自 https://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers/ 的示例
def http_get(url)
f = Fiber.current
http = EventMachine::HttpRequest.new(url).get
# resume fiber once http call is done
http.callback { f.resume(1,http) }
http.errback { f.resume(2,http) }
return Fiber.yield
end
EventMachine.run do
Fiber.new{
result = http_get('https://www.google.com/')
if (result[0] ==2)
puts "error"
return # using break has the error 'break from proc-closure (LocalJumpError)' too
end
result = http_get('https://www.google.com/search?q=eventmachine')
page = result[1]
puts "Fetched page 2: #{page.response}"
}.resume
end
我收到了错误。
---- 更新 2 ----
通过我得到的答案和 cmets,我找到了这个 How can I return something early from a block?
这个Why does the break statement in ruby behave differently when using Proc.new v. the ampersand sign? 解释了为什么 break 也不起作用。引用“break 应该让块的调用者返回,但 Proc.new 已经返回。”
return vs break vs next 绝对是 ruby 新手的障碍
【问题讨论】:
-
“我不能在 Ruby 中的
if块内返回”——当然可以。 -
我现在得到“未捕获的异常:意外返回”
-
您能说明您遇到问题的方法吗?只要您在方法中,提前返回通常应该有效。
-
另外,我想说
ifs 已经是代码异味了,但是as manyifs 是一个明确的信号,必须尽快重构代码。 -
@JörgWMittag 请查看我的更新问题。
标签: ruby coding-style guard-clause