有更好的方法来处理这个问题吗?
我已经多次遇到过这种特殊的模式,为了使其正常工作而必须声明标志变量有点烦人。有没有一种更简单的方法来安排我没有看到的代码?
flag = true
if x.is_okay?
some_stuff_that_needs_x_to_be_okay
if some_condition_that_depended_on_x
actually_important_stuff
flag = false
end
end
if flag
do_something_when_important_stuff_did_not
end
I've come across this particular pattern a few times now and it's somewhat annoying to have to declare the flag variable in order for this to work correctly. Is there a simpler way to arrange this code that I'm just not seeing?
flag = true
if x.is_okay?
some_stuff_that_needs_x_to_be_okay
if some_condition_that_depended_on_x
actually_important_stuff
flag = false
end
end
if flag
do_something_when_important_stuff_did_not
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种没有标志变量的方法。如果
do_something_when_important_stuff_did_not
是一个简单的语句(例如方法调用),那么这是合理的。Here's one way without the flag variable. It's reasonable if
do_something_when_important_stuff_did_not
is a single, simple statement, such as a method call.