//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
未捕获异常:意外返回…`block(2级)in':意外返回(LocalJumpError)
----更新-----
def early(input)
if (input <0)
puts 'should >0'
return
end
puts 'good'
end
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