# Return in blocks

**URL:** <https://www.ruby-forum.com/t/return-in-blocks/48428>\
**Category:** Ruby\
**Created:** [November 20, 2005, 11:31am UTC](https://www.ruby-forum.com/t/return-in-blocks/48428 "2005-11-20T11:31:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyndon.samson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e9bcb4/32.png) [@lyndon.samson](https://www.ruby-forum.com/u/lyndon.samson)\
**Post date:** [November 20, 2005, 11:31am UTC](https://www.ruby-forum.com/t/return-in-blocks/48428/1 "2005-11-20T11:31:09Z")

</div>

Whats the official word on using return to return a value in a block,  
rather than just the value of the last expression?

Using ruby 1.8.2 (2004-12-25) [i386-mswin32] I see strange behaviour (  
silent crash ) when explicitly using ‘return’ to return from a block,  
yet otherwise the code runs fine.

running under ntsd

eax=74fd5054 ebx=7ffdf000 ecx=0241cdac edx=00000000 esi=77f8dd80  
edi=00000000  
eip=77f8dd8b esp=0240fe2c ebp=0240fef4 iopl=0 nv up ei pl zr na  
po nc  
cs=001b ss=0023 ds=0023 es=0023 fs=0038 gs=0000  
efl=00000246  
ntdll!NtTerminateProcess+b:  
77f8dd8b c20800 ret 0x8

Which I can’t interpret as anything in particular.

---

<div class="post-metadata">

**Author:** ![lyndon.samson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e9bcb4/32.png) [@lyndon.samson](https://www.ruby-forum.com/u/lyndon.samson)\
**Post date:** [November 20, 2005, 12:07pm UTC](https://www.ruby-forum.com/t/return-in-blocks/48428/2 "2005-11-20T12:07:22Z")

</div>

Hi,

In message “Re: return in blocks”  
on Sun, 20 Nov 2005 19:29:40 +0900, Lyndon S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Whats the official word on using return to return a value in a block,  
|rather than just the value of the last expression?

Usually use “break ” to pass a value from a block.

|Using ruby 1.8.2 (2004-12-25) [i386-mswin32] I see strange behaviour (  
|silent crash ) when explicitly using ‘return’ to return from a block,  
|yet otherwise the code runs fine.

Show us a concrete code to reproduce your crash, please, preferably  
using more recent version, at least 1.8.3.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![lyndon.samson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e9bcb4/32.png) [@lyndon.samson](https://www.ruby-forum.com/u/lyndon.samson)\
**Post date:** [November 21, 2005, 5:06am UTC](https://www.ruby-forum.com/t/return-in-blocks/48428/3 "2005-11-21T05:06:08Z")

</div>

Lyndon S. a écrit :

> Whats the official word on using return to return a value in a block,  
> rather than just the value of the last expression?

return in a block doesn’t just return the block, it returns the method  
where the block has been created.

Exemple:

def my\_method  
test = [“one”, “two”, “three”]  
test.each do |e|  
if e == “three”  
return “my\_method returned”  
end  
puts e  
end  
puts “will it ever goes here?”  
end

puts my\_method

output:

one  
two  
my\_method\_returned

–  
Lionel T.

Personal web site: [http://users.skynet.be/lthiry/](http://users.skynet.be/lthiry/)

---

<div class="post-metadata">

**Author:** ![lyndon.samson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e9bcb4/32.png) [@lyndon.samson](https://www.ruby-forum.com/u/lyndon.samson)\
**Post date:** [November 21, 2005, 5:18am UTC](https://www.ruby-forum.com/t/return-in-blocks/48428/4 "2005-11-21T05:18:12Z")

</div>

On 11/21/05, Lionel T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Lyndon S. a Ã©crit :
> 
> > Whats the official word on using return to return a value in a block,  
> > rather than just the value of the last expression?
> 
> return in a block doesn’t just return the block, it returns the method  
> where the block has been created.
> 
> Hence my silence, I’m sure when I check this will turn out to be what  
> happened.

Apologies for the noise 🙂
