Edit:
The desired effect, turns out, can be achieved much more simply by calling break inside the inner loop (will only terminate that loop):
bank.branches do |branch|
branch.employees.each do |employee|
break if employee.name = "John Doe"
end
end
Here is what @steenslag was getting at written with Ruby begin-rescue-end block:
letters = [%w(a b c),%w(d e f),%w(g h i)]
# => [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
letters.each do |trine|
begin
trine.each do |letter|
raise "Breaking out of inner cycle." if letter == "e"
puts letter
end
rescue
next
end
end
# => abcdghi
So your example is:
bank.branches do |branch|
branch.employees.each do |employee|
begin
raise "Breaking out of inner cycle." if employee.name = "John Doe"
rescue
next
end
end
end
NEXT BRANCH
is very bad pseudocode for breaking out of a parent loop. Asnext
andbreak
are distinct keywords.