[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to break out deeply nested loops? (newbie

Talha Oktay

3/30/2006 6:16:00 PM

I sometimes need to break out, next or redo deeply nested loops in different
levels instead of the innermost enclosing scope. Is there a facility to
label blocks or loops and redo, next or break to the labeled blocks or
loops as in some other programming languages?

Thanks in advance.
3 Answers

Chris Alfeld

3/30/2006 6:54:00 PM

0

Not nicely.

This is discussed in the Pickaxe. For example

catch "BreakOuterLoop" do
for i in 1..10
print "out #{i}\n"
for j in 1..10
print "in #{j}\n"
throw "BreakOuterLoop" if i+j > 16
end
end
end

On 3/30/06, Talha Oktay <toktay@gmail.com> wrote:
> I sometimes need to break out, next or redo deeply nested loops in different
> levels instead of the innermost enclosing scope. Is there a facility to
> label blocks or loops and redo, next or break to the labeled blocks or
> loops as in some other programming languages?
>
> Thanks in advance.
>
>


james_b

3/30/2006 6:59:00 PM

0

Talha Oktay wrote:
> I sometimes need to break out, next or redo deeply nested loops in different
> levels instead of the innermost enclosing scope. Is there a facility to
> label blocks or loops and redo, next or break to the labeled blocks or
> loops as in some other programming languages?
>
> Thanks in advance.
>

throw/catch

http://www.ruby-doc.org/core/classes/Kernel.ht...


--
James Britt



Simon Kröger

3/30/2006 8:07:00 PM

0

Talha Oktay schrieb:
> I sometimes need to break out, next or redo deeply nested loops in different
> levels instead of the innermost enclosing scope. Is there a facility to
> label blocks or loops and redo, next or break to the labeled blocks or
> loops as in some other programming languages?
>
> Thanks in advance.


You may use throw, catch or continuations (for the fun of it) or just
wrap some of your inner loops in their own method and use return.
(i prefer the last version most of the time)

cheers

Simon