# Passing block to another function

**URL:** <https://www.ruby-forum.com/t/passing-block-to-another-function/55399>\
**Category:** Ruby\
**Created:** [March 20, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/passing-block-to-another-function/55399 "2006-03-20T20:48:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shea\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Shea\_M](https://www.ruby-forum.com/u/Shea_M)\
**Post date:** [March 20, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/passing-block-to-another-function/55399/1 "2006-03-20T20:48:59Z")

</div>

I have a function:

def worker( &p\_block )  
…  
p\_block.call( my\_data )  
…  
end

I have a wrapper function, that I would like to use to call ‘worker’.  
But I would like the caller of ‘wrapper’ to be able to specify the block  
‘wrapper’ will use. i.e.,

def wrapper( &p\_block )  
…  
worker{ p\_block }  
end

I suspect that this is possible, but I can’t seem to nail the syntax.

?

~S

---

<div class="post-metadata">

**Author:** ![Shea\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Shea\_M](https://www.ruby-forum.com/u/Shea_M)\
**Post date:** [March 20, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/passing-block-to-another-function/55399/2 "2006-03-20T20:52:14Z")

</div>

On Tue, Mar 21, 2006 at 04:48:50AM +0900, Shea M. wrote:

> ‘wrapper’ will use. i.e.,
> 
> def wrapper( &p\_block )  
> …  
> worker{ p\_block }  
> end
> 
> I suspect that this is possible, but I can’t seem to nail the syntax.

def worker(&block)  
block.call(data)  
end

def wrapper(&block)  
worker(&block)  
end

marcel

---

<div class="post-metadata">

**Author:** ![Shea\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Shea\_M](https://www.ruby-forum.com/u/Shea_M)\
**Post date:** [March 20, 2006, 8:53pm UTC](https://www.ruby-forum.com/t/passing-block-to-another-function/55399/3 "2006-03-20T20:53:56Z")

</div>

Shea M. wrote:

> I suspect that this is possible, but I can’t seem to nail the syntax.

Ok,

The following works, is this the  
def worker( p\_num, &p\_block )  
for i in 0…p\_num  
p\_block.call( i )  
end  
end

def five\_times( &p\_block )  
worker( 5 ){ |x| p\_block.call(x) }  
end

five\_times { |i| puts “#{i}. hello” }

…

~S

---

<div class="post-metadata">

**Author:** ![Shea\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Shea\_M](https://www.ruby-forum.com/u/Shea_M)\
**Post date:** [March 20, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/passing-block-to-another-function/55399/4 "2006-03-20T21:33:56Z")

</div>

> It makes sense once you “wrap” your head around it 😉
> 
> (Also, you might want, if possible, to use yield in worker, as well.  
> It’s more efficient.)

Yeah, it seems dead obvious _now_. That is good to know about ‘yield’  
being more efficient. I always used the named Proc, as I prefer it  
syntactically (and I always mispell yeild 😃 ). I will start using  
yield.

~S

---

<div class="post-metadata">

**Author:** ![Shea\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Shea\_M](https://www.ruby-forum.com/u/Shea_M)\
**Post date:** [March 20, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/passing-block-to-another-function/55399/5 "2006-03-20T20:55:04Z")

</div>

Shea M. wrote:

> ‘wrapper’ will use. i.e.,
> 
> def wrapper( &p\_block )  
> …  
> worker{ p\_block }  
> end
> 
> I suspect that this is possible, but I can’t seem to nail the syntax.

def wrapper  
worker {yield}  
end

It makes sense once you “wrap” your head around it 😉

(Also, you might want, if possible, to use yield in worker, as well.  
It’s more efficient.)

---

<div class="post-metadata">

**Author:** ![Shea\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Shea\_M](https://www.ruby-forum.com/u/Shea_M)\
**Post date:** [March 20, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/passing-block-to-another-function/55399/6 "2006-03-20T22:47:52Z")

</div>

Shea M. wrote:

> ~S

Just found my benchmark to back this assertion up:

$ cat yield-vs-proc.rb  
require ‘benchmark’

def outer11(&bl)  
inner1(&bl)  
end

def outer12(&bl)  
inner2(&bl)  
end

def outer21  
inner1 {yield}  
end

def outer22  
inner2 {yield}  
end

def inner1(&bl)  
bl.call  
end

def inner2  
yield  
end

n = 100000

Benchmark.bmbm(10) do |rpt|  
rpt.report(“outer11”) do  
n.times {outer11{}}  
end

rpt.report(“outer12”) do  
n.times {outer12{}}  
end

rpt.report(“outer21”) do  
n.times {outer21{}}  
end

rpt.report(“outer22”) do  
n.times {outer22{}}  
end  
end

**END**

Output:

Rehearsal ---------------------------------------------  
outer11 0.890000 0.010000 0.900000 ( 0.894500)  
outer12 0.370000 0.000000 0.370000 ( 0.364880)  
outer21 0.770000 0.000000 0.770000 ( 0.776638)  
outer22 0.170000 0.000000 0.170000 ( 0.163393)  
------------------------------------ total: 2.210000sec

```
            user system total real

```

outer11 0.490000 0.000000 0.490000 ( 0.491969)  
outer12 0.400000 0.000000 0.400000 ( 0.396264)  
outer21 0.760000 0.000000 0.760000 ( 0.764508)  
outer22 0.160000 0.000000 0.160000 ( 0.161982)
