Alex Rivera | Logout

How to break from nested loops in Ruby?

Asked 2011-03-13T01:44:47.827
73

assume the following ruby code:

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

NEXT BRANCH is of course pseudocode. is there a way that i can break out of a parent loop, the way one can do so in Perl, for example (by employing loop labels)?

thanks in advance.

Edit
Report

1 Answer

11
while c1
 while c2
    # execute code
    do_break = true if need_to_break_out_of_parent_loop
 end
 break if do_break
end
answered 2012-09-29T10:22:55.773

Your Answer