# Passing a block to a block

**URL:** <https://www.ruby-forum.com/t/passing-a-block-to-a-block/152488>\
**Category:** Ruby\
**Created:** [November 4, 2008, 2:42pm UTC](https://www.ruby-forum.com/t/passing-a-block-to-a-block/152488 "2008-11-04T14:42:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [November 4, 2008, 2:42pm UTC](https://www.ruby-forum.com/t/passing-a-block-to-a-block/152488/1 "2008-11-04T14:42:51Z")

</div>

I notice that if you pass a block to a block, you can’t use ‘yield’ to  
yield to that block:

$ cat yield\_in\_block.rb  
foo = lambda { |e| puts e }  
blk = lambda { |e| yield e }  
blk.call(123, &foo)

$ ruby yield\_in\_block.rb  
yield\_in\_block.rb:2: no block given (LocalJumpError)  
from yield\_in\_block.rb:3  
$ ruby19 yield\_in\_block.rb  
yield\_in\_block.rb:2:in `block in <main>': no block given (yield) (LocalJumpError) from yield_in_block.rb:3:in`call’  
from yield\_in\_block.rb:3:in `’

What’s the reason for this? Does ‘yield’ only invoke the top-level block  
passed into a method?

ruby1.9 is happy but only if I explicitly reference the block to yield.

foo = lambda { |e| puts e }  
blk = lambda { |e,&y| y[e] }  
blk.call(123, &foo)

Thanks,

Brian.

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [November 4, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/passing-a-block-to-a-block/152488/2 "2008-11-04T20:20:46Z")

</div>

Brian C. wrote:

> I notice that if you pass a block to a block, you can’t use ‘yield’ to  
> yield to that block:
> 
> $ cat yield\_in\_block.rb  
> foo = lambda { |e| puts e }  
> blk = lambda { |e| yield e }  
> blk.call(123, &foo)

blk is not a block. It is a Proc object or closure.  
(Note that it isn’t a Lambda object. Lambda is  
for Lisp weenies.)

> passed into a method?
> 
> ruby1.9 is happy but only if I explicitly reference the block to yield.
> 
> foo = lambda { |e| puts e }  
> blk = lambda { |e,&y| y[e] }  
> blk.call(123, &foo)

foo = proc{|x| puts x }  
==\>#Proc:0x02b2ae60@:1(irb)  
bar = proc{|x,prc| prc[x] }  
==\>#Proc:0x02b24ac4@:2(irb)  
bar[88, foo]  
88  
==\>nil

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [November 4, 2008, 9:09pm UTC](https://www.ruby-forum.com/t/passing-a-block-to-a-block/152488/3 "2008-11-04T21:09:02Z")

</div>

William J. wrote:

> foo = proc{|x| puts x }  
> ==\>#Proc:0x02b2ae60@:1(irb)  
> bar = proc{|x,prc| prc[x] }  
> ==\>#Proc:0x02b24ac4@:2(irb)  
> bar[88, foo]

Indeed, there’s no problem when you pass them around and invoke them  
explicitly.

My question (and I apologise if I didn’t word it clearly) was regarding  
the ‘yield’ keyword, to make a callback to a hidden proc which is not  
one of the explicit arguments. e.g.

def foo(\*a,&b)  
yield 123 # similar to b.call(123)  
end

pr = Proc.new { |x| puts x }  
foo(&pr)

This works fine as shown when passing to a method, but not when passing  
to another Proc.
