在Perl中,有能力打破这样的外部循环:
AAA: for my $stuff (@otherstuff) {
         for my $foo (@bar) {
             last AAA if (somethingbad());
         }
      }
(语法可能有误),它使用循环标签从内部循环内部中断外部循环.Ruby中有类似的东西吗?
Chr*_*nch 106
考虑throw/catch.通常,下面代码中的外部循环将运行五次,但是使用throw可以将其更改为您喜欢的任何内容,并在此过程中将其分解.考虑这个完全有效的ruby代码:
catch (:done) do
  5.times { |i|
    5.times { |j|
      puts "#{i} #{j}"
      throw :done if i + j > 5
    }
  }
end
Jör*_*tag 36
你想要的是非本地控制流,Ruby有几种选择:
throw/catch延续
优点:
GOTO.缺点:
例外
优点:
缺点:
throw/catch
这是(粗略地)它的样子:
catch :aaa do
  stuff.each do |otherstuff|
    foo.each do |bar|
      throw :aaa if somethingbad
    end
  end
end
优点:
StopIteration例外终止.缺点:
sep*_*p2k 30
不,没有.
你的选择是: