3

Here is the Sample code,

while true
   while true
      exit all loops when condition true
   end
end

Can someone tell me if is it possible here to exit first loop when second loop breaks, but then I want to use only one break command and no raise.

Nirupa
  • 787
  • 1
  • 8
  • 20
Vijay
  • 443
  • 4
  • 13
  • Wrap them in a method and return from that. Or use throw/catch. – Sergio Tulentsev Apr 22 '15 at 12:28
  • Ya it's a good idea, but is there any other way to solve my problem. – Vijay Apr 22 '15 at 12:32
  • What do you mean? There are two other ways right there. – Sergio Tulentsev Apr 22 '15 at 12:32
  • I don't want to raise any exception. I want to know any other logics to do this. – Vijay Apr 22 '15 at 12:33
  • throw/catch is not exception handling. You confused them with raise/rescue. – Sergio Tulentsev Apr 22 '15 at 12:34
  • Ya i agree my mistake. Is there a way to do this without raise and more than one break . Also i agree the answer you gave is very good. But i am looking, is there any other way to solve this problem. I mean any ruby command can do this. – Vijay Apr 22 '15 at 12:38
  • Not sure what you mean. If you want the functionality (breaking out of several loops), you have it in my answer. throw/catch is such command. – Sergio Tulentsev Apr 22 '15 at 12:39
  • possible duplicate of [How to break outer cycle in Ruby?](http://stackoverflow.com/questions/1352120/how-to-break-outer-cycle-in-ruby) ...Maybe I should have Googled for duplicates before failing at answering. Oops. – Nic Apr 22 '15 at 12:40
  • Ya you are right , it is the best solution. @SergioTulentsev – Vijay Apr 22 '15 at 12:42

2 Answers2

7

You know what's better than using only one break? Not using any at all! :)

Little-used throw/catch is good here

catch(:done) do 
  while cond1
    while cond2
      throw :done if condition
    end
  end
end

For more information, see the docs on throw and catch.

Nic
  • 6,211
  • 10
  • 46
  • 69
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • ...Gosh darnit, you just _had_ to write that up in the time I spent not reading the post, didn't you? – Nic Apr 22 '15 at 12:37
  • I didn't know throw/catch was distinct from exceptions until... oh, about three minutes ago. I'm too used to Java. – Nic Apr 22 '15 at 12:38
1

Alright, so apparently boolean flags are a no-go. Oops.

The other thing that pops to mind is catching an error, but you said you don't want to do that, or wrap it in a method and return. Honestly, there doesn't seem to be a way, but here's the simplest I could come up with:

catch (:exit) do
    while true
        while true
            throw :exit if condition
        end
    end
end

You could also throw an exception, but that seems dirty. Here's the code to do it, though:

begin
    while true
        while true
            raise "Exiting loops" if condition
        end
    end
rescue
    #Code to go after the loop
end

Lastly, you could wrap the whole thing in a method and return from that method:

def my_descriptive_method_name()
    while true
        while true
            return if condition
        end
    end
end
Nic
  • 6,211
  • 10
  • 46
  • 69