# Question about blocks

**URL:** <https://rubytalk.org/t/question-about-blocks/9773>\
**Category:** ruby-talk\
**Created:** [20 January 2004 20:37 UTC](https://rubytalk.org/t/question-about-blocks/9773 "2004-01-20T20:37:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ferenc\_Engard1](https://avatars.discourse-cdn.com/v4/letter/f/c6cbf5/32.png) [@Ferenc\_Engard1](https://rubytalk.org/u/Ferenc_Engard1)\
**Post date:** [20 January 2004 20:37 UTC](https://rubytalk.org/t/question-about-blocks/9773/1 "2004-01-20T20:37:48Z")

</div>

Hello,

How can I return a value from a block? “return” immediately returns from  
the caller method, “break” bypasses the called method (which yielded the  
block).

I have read something about this, but cannot remember where. I know the  
syntax changed between 1.6.8 and 1.8.

Ferenc

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard1](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@Simon\_Strandgaard1](https://rubytalk.org/u/Simon_Strandgaard1)\
**Post date:** [20 January 2004 21:05 UTC](https://rubytalk.org/t/question-about-blocks/9773/2 "2004-01-20T21:05:02Z")

</div>

> How can I return a value from a block? “return” immediately returns from  
> the caller method, “break” bypasses the called method (which yielded the  
> block).
> 
> I have read something about this, but cannot remember where. I know the  
> syntax changed between 1.6.8 and 1.8.

How about this code?

> ruby a.rb  
> before  
> after result=42  
> expand -t2 a.rb  
> def test(&block)  
> puts “before”  
> res = nil  
> begin  
> res = block.call  
> rescue LocalJumpError =\> e  
> res = e.exit\_value  
> end  
> puts “after result=#{res}”  
> end

test { return 42 }

> **···**
>
> On Wed, 21 Jan 2004 05:37:48 +0900, Ferenc Engard wrote:
> 
> > 
> 
> –  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [20 January 2004 22:49 UTC](https://rubytalk.org/t/question-about-blocks/9773/3 "2004-01-20T22:49:12Z")

</div>

Hi,

> **···**
>
> At Wed, 21 Jan 2004 05:37:48 +0900, Ferenc Engard wrote:
> 
> > How can I return a value from a block? “return” immediately returns from  
> > the caller method, “break” bypasses the called method (which yielded the  
> > block).
> 
> next value
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Jamis\_Buck4](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@Jamis\_Buck4](https://rubytalk.org/u/Jamis_Buck4)\
**Post date:** [20 January 2004 21:11 UTC](https://rubytalk.org/t/question-about-blocks/9773/4 "2004-01-20T21:11:37Z")

</div>

Simon Strandgaard wrote:

> > How can I return a value from a block? “return” immediately returns from  
> > the caller method, “break” bypasses the called method (which yielded the  
> > block).
> > 
> > I have read something about this, but cannot remember where. I know the  
> > syntax changed between 1.6.8 and 1.8.
> 
> How about this code?
> 
> > ruby a.rb  
> > before  
> > after result=42  
> > expand -t2 a.rb  
> > def test(&block)  
> > puts “before”  
> > res = nil  
> > begin  
> > res = block.call  
> > rescue LocalJumpError =\> e  
> > res = e.exit\_value  
> > end  
> > puts “after result=#{res}”  
> > end
> 
> test { return 42 }
> 
> > 
> 
> –  
> Simon Strandgaard

By way of explanation, I believe the difference is this: when the block  
is invoked implicitly (via ‘yield’) a ‘return’ or ‘break’ will affect a  
scope greater than itself (not sure if my terminology is correct here).  
When the block is invoked explicitly (via the ‘call’ method of Proc),  
then ‘return’ and ‘break’ affect only the block itself.

Perhaps some Ruby guru could verify whether this is accurate or not.  
This seems to be the behavior as I’ve seen it, at least according to my  
few forays into the Ruby source code.

> **···**
>
> > On Wed, 21 Jan 2004 05:37:48 +0900, Ferenc Engard wrote:
> 
> –  
> Jamis Buck  
> jgb3@email.byu.edu
> 
> ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S\*)|le.l(…)e|#!(\S\*)/) {|r| a \<\< r.compact.first };puts “\n\>#{a.join(%q/ /)}\<\n\n”’

---

<div class="post-metadata">

**Author:** ![Charles\_Mills](https://avatars.discourse-cdn.com/v4/letter/c/b77776/32.png) [@Charles\_Mills](https://rubytalk.org/u/Charles_Mills)\
**Post date:** [20 January 2004 23:03 UTC](https://rubytalk.org/t/question-about-blocks/9773/5 "2004-01-20T23:03:33Z")

</div>

> > How can I return a value from a block? “return” immediately returns from  
> > the caller method, “break” bypasses the called method (which yielded the  
> > block).
> > 
> > I have read something about this, but cannot remember where. I know the  
> > syntax changed between 1.6.8 and 1.8.
> 
> How about this code?

I am using cygwin on XP and I get the following :  
$ ./a.rb  
before

> > ruby a.rb  
> > before  
> > after result=42  
> > expand -t2 a.rb  
> > def test(&block)  
> > puts “before”  
> > res = nil  
> > begin  
> > res = block.call  
> > rescue LocalJumpError =\> e  
> > res = e.exit\_value  
> > end  
> > puts “after result=#{res}”  
> > end
> 
> test { return 42 }
> 
> > 
> 
> –  
> Simon Strandgaard

here is the contents of my a.rb:  
#!/usr/bin/ruby

def test(&block)  
puts “before”  
res = nil  
begin  
res = block.call  
rescue LocalJumpError =\> e  
res = e.exit\_value  
end  
puts “after result=#{res}”  
end

test { return 42 }

> **···**
>
> On Wed, 21 Jan 2004, Simon Strandgaard wrote:
> 
> > On Wed, 21 Jan 2004 05:37:48 +0900, Ferenc Engard wrote:
> 
> –  
> Charlie

---

<div class="post-metadata">

**Author:** ![Ferenc\_Engard1](https://avatars.discourse-cdn.com/v4/letter/f/c6cbf5/32.png) [@Ferenc\_Engard1](https://rubytalk.org/u/Ferenc_Engard1)\
**Post date:** [21 January 2004 21:01 UTC](https://rubytalk.org/t/question-about-blocks/9773/6 "2004-01-21T21:01:19Z")

</div>

> > How can I return a value from a block? “return” immediately returns from  
> > the caller method, “break” bypasses the called method (which yielded the  
> > block).
> 
> next value

Thanks, this is it. The documentation should be updated! 🙂

Ferenc

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard1](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@Simon\_Strandgaard1](https://rubytalk.org/u/Simon_Strandgaard1)\
**Post date:** [20 January 2004 23:15 UTC](https://rubytalk.org/t/question-about-blocks/9773/7 "2004-01-20T23:15:00Z")

</div>

> > How about this code?
> 
> I am using cygwin on XP and I get the following :  
> $ ./a.rb  
> before

Wierd… is this a bug? am I doing something wrong?

> ruby -v  
> ruby 1.8.1 (2003-12-22) [i386-freebsd5.1]

> **···**
>
> On Wed, 21 Jan 2004 08:03:33 +0900, Charles Mills wrote:
> 
> > On Wed, 21 Jan 2004, Simon Strandgaard wrote:
> 
> –  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Charles\_Mills](https://avatars.discourse-cdn.com/v4/letter/c/b77776/32.png) [@Charles\_Mills](https://rubytalk.org/u/Charles_Mills)\
**Post date:** [20 January 2004 23:19 UTC](https://rubytalk.org/t/question-about-blocks/9773/8 "2004-01-20T23:19:19Z")

</div>

Here is my version info:  
$ ruby -v  
ruby 1.8.0 (2003-08-04) [i386-cygwin]

I am not sure how ruby is suppost to behave in this instance.

> **···**
>
> On Wed, 21 Jan 2004, Simon Strandgaard wrote:
> 
> > On Wed, 21 Jan 2004 08:03:33 +0900, Charles Mills wrote:
> > 
> > > On Wed, 21 Jan 2004, Simon Strandgaard wrote:
> > > 
> > > > How about this code?
> > > 
> > > I am using cygwin on XP and I get the following :  
> > > $ ./a.rb  
> > > before
> > 
> > Wierd… is this a bug? am I doing something wrong?
> > 
> > > ruby -v  
> > > ruby 1.8.1 (2003-12-22) [i386-freebsd5.1]
> > 
> > –  
> > Simon Strandgaard
> 
> –  
> Charlie
