This has god me thinking about how, break, next and redo work in the scope
of blocks.

E.g.

def myfunc
10.times { | i| puts "Calling block with #{i}"; yield( i ) }
end

myfunc() { |i| puts "Block called with #{i}" i; break  if i > 4 }

Works as expected.  Although I put no code in the outter loop to action the
break. How far up the stack would this break go?

def myfunc2
yield 1
 yield 2
 yield 3
end

 myfunc2() {|i| puts i; break if i==2 }

also works as per the first example. However in this case it appears to have
exited the calling fucntion.

What does ruby actually do when a break, or next or redo is encountered?

Thanks




----- Original Message -----
From: "Robert Feldt" <feldt / ce.chalmers.se>
To: "ruby-talk ML" <ruby-talk / ruby-lang.org>
Sent: Wednesday, November 14, 2001 11:33 AM
Subject: [ruby-talk:25094] Re: exiting blox


> On Wed, 14 Nov 2001, Alan Chen wrote:
>
> > break also stops any subsequent yields to the block.  I think Niko
> > just wants to skip the rest of the block for that yield.
> >
> irb(main):003:0> [1,2,3].each {|e| e==2 ? break : p(e)}
> 1
> nil
> irb(main):004:0> [1,2,3].each {|e| e==2 ? next : p(e)}
> 1
> 3
> [1, 2, 3]
>
> /Robert
>
>