[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Question about exiting from a block

Guoliang Cao

6/4/2009 9:35:00 PM

def test
puts 'before'
yield 2
puts 'after'
end

test do |i|
XXX if i > 1
# a lot of code appears below
end


I have code above and wonder if it is possible to use
return/break/anything to stop execution of block and return control to
test() to execute code after yield. I know it is impossible in 1.8. How
about 1.9? Does anyone feel this is an important feature to have?

Thank you.
Guoliang Cao
--
Posted via http://www.ruby-....

7 Answers

Joel VanderWerf

6/4/2009 9:49:00 PM

0

Guoliang Cao wrote:
> def test
> puts 'before'
> yield 2
> puts 'after'
> end
>
> test do |i|
> XXX if i > 1
> # a lot of code appears below
> end
>
>
> I have code above and wonder if it is possible to use
> return/break/anything to stop execution of block and return control to
> test() to execute code after yield. I know it is impossible in 1.8. How
> about 1.9? Does anyone feel this is an important feature to have?

It is possible in 1.8, using the next keyword:

def test
puts 'before'
yield 2
puts 'after'
end

test do |i|
next if i > 1
puts "a lot of code"
end

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

matt

6/5/2009 3:48:00 AM

0

Guoliang Cao <gcao@vonage.com> wrote:

> def test
> puts 'before'
> yield 2
> puts 'after'
> end
>
> test do |i|
> XXX if i > 1
> # a lot of code appears below
> end
>
>
> I have code above and wonder if it is possible to use
> return/break/anything to stop execution of block and return control to
> test() to execute code after yield. I know it is impossible in 1.8.

Why is it impossible? Doesn't "break" do what you need? (And remember,
"break" and "next" can even return a value from a block to the yielder.)
m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Joel VanderWerf

6/5/2009 7:03:00 AM

0

matt neuburg wrote:
> Guoliang Cao <gcao@vonage.com> wrote:
>
>> def test
>> puts 'before'
>> yield 2
>> puts 'after'
>> end
>>
>> test do |i|
>> XXX if i > 1
>> # a lot of code appears below
>> end
>>
>>
>> I have code above and wonder if it is possible to use
>> return/break/anything to stop execution of block and return control to
>> test() to execute code after yield. I know it is impossible in 1.8.
>
> Why is it impossible? Doesn't "break" do what you need? (And remember,
> "break" and "next" can even return a value from a block to the yielder.)

Nope, break will skip the "after" line.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Guoliang Cao

6/5/2009 12:18:00 PM

0

Joel VanderWerf wrote:
> Guoliang Cao wrote:
>>
>>
>> I have code above and wonder if it is possible to use
>> return/break/anything to stop execution of block and return control to
>> test() to execute code after yield. I know it is impossible in 1.8. How
>> about 1.9? Does anyone feel this is an important feature to have?
>
> It is possible in 1.8, using the next keyword:
>
> def test
> puts 'before'
> yield 2
> puts 'after'
> end
>
> test do |i|
> next if i > 1
> puts "a lot of code"
> end

This is great! I never thought of using 'next'.

Thank you!
--
Posted via http://www.ruby-....

Brian Candler

6/5/2009 12:22:00 PM

0

Keeping it simple:

def test
puts 'before'
yield 2
puts 'after'
end

test do |i|
unless i > 1
puts "a lot of code here"
end
end

There's also throw/catch:

test do |i|
catch(:done) do
throw(:done) if i > 1
puts "a lot of code here"
end
end
--
Posted via http://www.ruby-....

Bertram Scharpf

6/5/2009 12:46:00 PM

0

Hi,

Am Freitag, 05. Jun 2009, 06:48:43 +0900 schrieb Joel VanderWerf:
> Guoliang Cao wrote:
>> I have code above and wonder if it is possible to use
>> return/break/anything to stop execution of block and return control to
>> test() to execute code after yield. I know it is impossible in 1.8. How
>> about 1.9?
>
> It is possible in 1.8, using the next keyword:

Slightly modified:

def f ; puts "A" ; puts yield ; puts "Z" ; "F" ; end

f { next "X" ; puts "Y" } #=> "F"
puts "-"
f { break "X" ; puts "Y" } #=> "X"

The output is:

A
X
Z
-
A

Will this work in 1.9, too?

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

matt

6/5/2009 3:13:00 PM

0

Joel VanderWerf <vjoel@path.berkeley.edu> wrote:

> matt neuburg wrote:
> > Guoliang Cao <gcao@vonage.com> wrote:
> >
> >> def test
> >> puts 'before'
> >> yield 2
> >> puts 'after'
> >> end
> >>
> >> test do |i|
> >> XXX if i > 1
> >> # a lot of code appears below
> >> end
> >>
> >>
> >> I have code above and wonder if it is possible to use
> >> return/break/anything to stop execution of block and return control to
> >> test() to execute code after yield. I know it is impossible in 1.8.
> >
> > Why is it impossible? Doesn't "break" do what you need? (And remember,
> > "break" and "next" can even return a value from a block to the yielder.)
>
> Nope, break will skip the "after" line.

Right, sorry about that. m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...