tags:

views:

1032

answers:

5

Hello. In Perl, there is an ability to break an outer cycle like this:

AAA: for my $stuff (@otherstuff) {
         for my $foo (@bar) {
             last AAA if (somethingbad());
         }
      }

(syntax may be wrong), which uses a loop label to break the outer loop from inside the inner loop. Is there anything similar in Ruby?

+9  A: 

No, there isn't.

Your options are:

  • put the loop in a method and use return to break from the outer loop
  • set or return a flag from the inner loop and then check that flag in the outer loop and break from it when the flag is set (which is kind of cumbersome)
  • use throw/catch to break out of the loop (which is kind of icky)
sepp2k
+13  A: 

Consider throw/catch. Normally the outside loop in the below code will run five times, but with throw you can change it to whatever you like, breaking it in the process.

catch (:done) do
  5.times { |i|
    5.times { |j|
      puts "#{i} #{j}"
      throw :done if i + j > 5
    }
  }
end
Chris Bunch
Personally I don't like using exception raising for normal code execution. It forces a programmer the follow multiple flows of logic.
Jeff Waltzer
I don't understand this comment. In the code snippet above there are no exceptions raised anywhere. There are only six messages sent in the entire piece of code: `catch`, `times`, `puts`, `throw`, `+` and `<=>`. I don't send of `raise` anywhere.
Jörg W Mittag
A: 

I know I will regret this in the morning but simply using a while loop could do the trick.

x=0
until x==10
  x+=1
  y=0
  until y==10
    y+=1
    if y==5 && x==3
      x,y=10,10
    end
  end
  break if x==10
  puts x
end

The if y==5 && x==3 is only an example of an expression turning true.

Jonas Elfström
It is now morning: please find regret.
John F. Miller
+1  A: 

Basically, what you want is non-local control-flow. Ruby has several options for doing non-local control-flow:

  • Continuations,
  • Exceptions and
  • throw/catch

Each of these has its pros and cons. Continuations are the standard mechanism for non-local control-flow. In fact, you can build any non-local control-flow (subroutines, procedures, functions, methods, coroutines, state machines, generators, conditions, exceptions) on top of them: they are pretty much the nicer twin of GOTO. Unfortunately, continuations are not a mandatory part of the Ruby Language Specification, which means that some implementations (XRuby, JRuby, Ruby.NET, IronRuby) don't implement them. So, you can't rely on them.

There is a paper that proves mathematically that Exceptions can be more powerful than Continuations. IOW: they can do everything that continuations can do, and more, so you can use them as a replacement for continuations. Pro: exceptions are universally available. Con: they are called "exceptions" which makes people think that they are "only for exceptional circumstances". This means three things: somebody reading your code might not understand it, the implementation might not be optimized for it (and, yes, exceptions are godawful slow in almost any Ruby implementation) and worst of all, you will get sick of all those people constantly, mindlessly babbling "exceptions are only for exceptional circumstances", as soon as they glance at your code. (Of course, they won't even try to understand what you are doing.)

Which leaves us with throw/catch. Pro: basically the same as exceptions. Con: the Ruby community hates them even more than using exceptions for control-flow.

This is (roughly) what it would look like:

catch :aaa do
  stuff.each do |otherstuff|
    foo.each do |bar|
      throw :aaa if somethingbad
    end
  end
end


Just for laughs: ironically, in Ruby 1.9, using exceptions for control-flow is actually part of the language specification! Loops, enumerators, iterators and such all use a StopIteration exception for termination.

Jörg W Mittag
+1  A: 

Perhaps this is what you want? (not tested)

stuff.find do |otherstuff|
  foo.find do
    somethingbad() && AAA
  end
end

The find method keeps looping until the block returns a non null value or the end of the list is hit.

Jeff Waltzer