2011-07-11 80 views
0

我现在已经遇到过这种特殊模式了,为了使它正常工作,必须声明标志变量有点烦人。有没有更简单的方法来安排我只是没有看到的代码?有没有更好的方法来处理这个问题?

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 

回答

0

这里没有标志变量的一种方式。如果do_something_when_important_stuff_did_not是一个单一的简单语句(如方法调用),这是合理的。

if x.is_okay? 
    some_stuff_that_needs_x_to_be_okay 
    if some_condition_that_depended_on_x 
    actually_important_stuff 
    else 
    do_something_when_important_stuff_did_not() 
    end 
else 
    do_something_when_important_stuff_did_not() 
end 
相关问题