# Passing a block around

**URL:** <https://www.ruby-forum.com/t/passing-a-block-around/80869>\
**Category:** Ruby\
**Created:** [December 31, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/passing-a-block-around/80869 "2006-12-31T20:51:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [December 31, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/passing-a-block-around/80869/1 "2006-12-31T20:51:27Z")

</div>

I want to pass a block to method foo then call method bar from foo and  
have bar yield to the block, I also want to be able to call bar  
directly with a block, how to do this neatly.

e.g. I want to be able to do

foo { |baz| do stuff with baz }  
bar { |baz| do other stuff with baz }

but when foo gets a block it passes it to bar in the background.

Thanks,  
James

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [December 31, 2006, 10:03pm UTC](https://www.ruby-forum.com/t/passing-a-block-around/80869/2 "2006-12-31T22:03:40Z")

</div>

Well, while I don’t quite understand everything you’ve said here, the  
mechanism for passing blocks around is simple:

def foo(&block)  
baz block  
end

def baz(&block)  
block.call(params)  
end

Hope that helps.

Jason

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [December 31, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/passing-a-block-around/80869/3 "2006-12-31T22:05:41Z")

</div>

Hi –

On Mon, 1 Jan 2007, Jason R. wrote:

> > bar { |baz| do other stuff with baz }
> > 
> > but when foo gets a block it passes it to bar in the background.
> 
> Well, while I don’t quite understand everything you’ve said here, the  
> mechanism for passing blocks around is simple:
> 
> def foo(&block)  
> baz block

You need &block rather than just block.

David
