gpt4 book ai didi

ruby - 如何打破 Ruby 中的嵌套循环?

转载 作者:数据小太阳 更新时间:2023-10-29 06:21:14 25 4
gpt4 key购买 nike

假设以下 ruby​​ 代码:

bank.branches do |branch|
branch.employees.each do |employee|
NEXT BRANCH if employee.name = "John Doe"
end
end

NEXT BRANCH 当然是伪代码。有没有一种方法可以打破父循环,例如在 Perl 中可以这样做(通过使用循环标签)?

提前致谢。

最佳答案

Catchthrow 可能是您正在寻找的:

bank.branches do |branch|
catch :missingyear do #:missingyear acts as a label
branch.employees.each do |employee|
(2000..2011).each do |year|
throw :missingyear unless something #break out of two loops
end
end
end #You end up here if :missingyear is thrown
end

关于ruby - 如何打破 Ruby 中的嵌套循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5286861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com