Next statement

I thought there was a way to use “next” (or similar) to escape an arbitrary number of loops. Akin to this:

list.each { |entry|
list2.each { |entry2|
next 2 if true
}
}

However, when I tried this I got:

mycode:###: parse error
next 2 if true
^

I can’t find anything at rubycentral on this - including the FAQ, the Book, and the Library Ref.

David Douthitt
CUNA & Affiliates
UNIX Systems Administrator
ddouthitt@cuna.coop

Of course you did – were you expecting ruby to automagically figure
out what ‘2’ refers to? :wink:

‘next’ just exits the innermost enclosing loop. To break out of
multiple loops, you want catch/throw, used like this:

catch(:done) {
list.each { |entry|
list2.each { |entry2|
throw :done if true
}
}
}

Dan

···

At Thu, 29 Aug 2002 07:07:48 +0900, David Douthitt wrote:

However, when I tried this I got:

mycode:###: parse error
next 2 if true
^


/^Dan Debertin$/
airboss@nodewarrior.org | Did I sleep a little too late,
www.nodewarrior.org | or am I awake? --Byrne

Er, sorry; I’m really not this stupid. I am, of course, referring to
the ‘break’ statement, not ‘next’. Sorry for the extra mailbox noise.

Dan

···

At Thu, 29 Aug 2002 07:13:40 +0900, Dan Debertin wrote:

‘next’ just exits the innermost enclosing loop.


/^Dan Debertin$/
airboss@nodewarrior.org | Did I sleep a little too late,
www.nodewarrior.org | or am I awake? --Byrne

Hi,

‘next’ just exits the innermost enclosing loop.

Er, sorry; I’m really not this stupid. I am, of course, referring to
the ‘break’ statement, not ‘next’. Sorry for the extra mailbox noise.

list.each { |entry|
next if catch(:next1) {
list2.each { |entry2|
throw :next1, true if true
}
false
}
}

I feel such magic number is painful, and a new syntax or
something else may be disirable.

···

At Thu, 29 Aug 2002 07:22:00 +0900, Dan Debertin wrote:


Nobu Nakada