# Question about exiting from a block

**URL:** <https://www.ruby-forum.com/t/question-about-exiting-from-a-block/169538>\
**Category:** Ruby\
**Created:** [June 4, 2009, 11:34pm UTC](https://www.ruby-forum.com/t/question-about-exiting-from-a-block/169538 "2009-06-04T23:34:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![guoliang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@guoliang](https://www.ruby-forum.com/u/guoliang)\
**Post date:** [June 4, 2009, 11:34pm UTC](https://www.ruby-forum.com/t/question-about-exiting-from-a-block/169538/1 "2009-06-04T23:34:40Z")

</div>

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 C.

---

<div class="post-metadata">

**Author:** ![guoliang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@guoliang](https://www.ruby-forum.com/u/guoliang)\
**Post date:** [June 4, 2009, 11:51pm UTC](https://www.ruby-forum.com/t/question-about-exiting-from-a-block/169538/2 "2009-06-04T23:51:58Z")

</div>

Guoliang C. 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

---

<div class="post-metadata">

**Author:** ![guoliang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@guoliang](https://www.ruby-forum.com/u/guoliang)\
**Post date:** [June 5, 2009, 5:50am UTC](https://www.ruby-forum.com/t/question-about-exiting-from-a-block/169538/3 "2009-06-05T05:50:49Z")

</div>

Guoliang C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

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.

---

<div class="post-metadata">

**Author:** ![guoliang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@guoliang](https://www.ruby-forum.com/u/guoliang)\
**Post date:** [June 5, 2009, 9:03am UTC](https://www.ruby-forum.com/t/question-about-exiting-from-a-block/169538/4 "2009-06-05T09:03:59Z")

</div>

matt neuburg wrote:

> > # 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.

---

<div class="post-metadata">

**Author:** ![guoliang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@guoliang](https://www.ruby-forum.com/u/guoliang)\
**Post date:** [June 5, 2009, 2:17pm UTC](https://www.ruby-forum.com/t/question-about-exiting-from-a-block/169538/5 "2009-06-05T14:17:30Z")

</div>

Joel VanderWerf wrote:

> Guoliang C. 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!

---

<div class="post-metadata">

**Author:** ![guoliang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@guoliang](https://www.ruby-forum.com/u/guoliang)\
**Post date:** [June 5, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/question-about-exiting-from-a-block/169538/6 "2009-06-05T14:22:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![guoliang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@guoliang](https://www.ruby-forum.com/u/guoliang)\
**Post date:** [June 5, 2009, 2:47pm UTC](https://www.ruby-forum.com/t/question-about-exiting-from-a-block/169538/7 "2009-06-05T14:47:20Z")

</div>

Hi,

Am Freitag, 05. Jun 2009, 06:48:43 +0900 schrieb Joel VanderWerf:

> Guoliang C. 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

---

<div class="post-metadata">

**Author:** ![guoliang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@guoliang](https://www.ruby-forum.com/u/guoliang)\
**Post date:** [June 5, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/question-about-exiting-from-a-block/169538/8 "2009-06-05T17:15:52Z")

</div>

Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > XXX if i \> 1
> 
> Nope, break will skip the “after” line.

Right, sorry about that. m.
