如何打破双重陈述?
a = 1
b = 2
c = 3
if a == 1
if b == 2
c = 5
d = 6
break
end
end
puts c
puts d
Run Code Online (Sandbox Code Playgroud)
产量
loop.rb:9: Invalid break
loop.rb: compile error (SyntaxError)
Run Code Online (Sandbox Code Playgroud)
你不能从内部打破if,你只能从内部循环和块中断.
如果你问的是如何打破两个嵌套循环,你可以catch结合使用throw- 这些与try和catch在其他语言中不一样.
catch(:stop) do
while some_cond
while other_cond
throw :stop
end
end
end
Run Code Online (Sandbox Code Playgroud)
当然,你总是可以设置一个标志或一些这样的标志来告诉外循环它也应该破坏.
| 归档时间: |
|
| 查看次数: |
2804 次 |
| 最近记录: |