# Yielding a block to another block

**URL:** <https://www.ruby-forum.com/t/yielding-a-block-to-another-block/67364>\
**Category:** Ruby\
**Created:** [September 2, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/yielding-a-block-to-another-block/67364 "2006-09-02T21:09:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [September 2, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/yielding-a-block-to-another-block/67364/1 "2006-09-02T21:09:02Z")

</div>

Hi all,

The mocking framework in rspec uses method\_missing to capture all the  
messages sent to it, comparing the message to a list of expected  
messages. For various reasons I’m trying to rework this so that  
instead of using method\_missing, we define the expected message on the  
mock on the fly.

For that to happen, setting expectations on the mock ends up invoking  
this:

```
  def define_expected_method(sym)
    (class << self; self; end). __send__ (:define_method, sym,

```

lambda {|\*args| message\_received(sym, \*args)})  
end

The Proc yields \*args, which are then passed to message\_received. How  
can I get the Proc to yield a block that is passed in with the call to  
:sym? I tried this:

lambda {|\*args, &block| …

but it throws an error. Any other ideas? Let me know if the question  
is not clear.

Thanks,  
David

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [September 2, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/yielding-a-block-to-another-block/67364/2 "2006-09-02T22:08:21Z")

</div>

David C. wrote:

> Hi all,
> 
> The mocking framework in rspec uses method\_missing to capture all the  
> messages sent to it, comparing the message to a list of expected  
> messages. For various reasons I’m trying to rework this so that  
> instead of using method\_missing, we define the expected message on the  
> mock on the fly.
> 
> For that to happen, setting expectations on the mock ends up invoking  
> this:
> 
> ```
> def define_expected_method(sym)
> (class << self; self; end). __send__ (:define_method, sym,
> 
> ```
> 
> lambda {|\*args| message\_received(sym, \*args)})  
> end
> 
> The Proc yields \*args, which are then passed to message\_received. How  
> can I get the Proc to yield a block that is passed in with the call to  
> :sym? I tried this:
> 
> lambda {|\*args, &block| …
> 
> but it throws an error. Any other ideas? Let me know if the question  
> is not clear.

You cannot currently do that with a block (1.9 and 2.0 will  
allow &block arguments). In the meanwhile, you can #eval the  
method into existence instead:

def define\_expected\_method(sym)  
class \<\< self; self; end. **send** :class\_eval, %{  
def #{sym}(\*args, &block)  
message\_received sym, \*args, &block # ?  
end  
}  
end

> Thanks,  
> David

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [September 2, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/yielding-a-block-to-another-block/67364/3 "2006-09-02T22:13:11Z")

</div>

Eero S. wrote:

> David C. wrote:
> 
> > Hi all,
> > 
> > The mocking framework in rspec uses method\_missing to capture all the  
> > messages sent to it, comparing the message to a list of expected  
> > messages. For various reasons I’m trying to rework this so that  
> > instead of using method\_missing, we define the expected message on the  
> > mock on the fly.
> > 
> > For that to happen, setting expectations on the mock ends up invoking  
> > this:
> > 
> > ```
> > def define_expected_method(sym)
> > (class << self; self; end). __send__ (:define_method, sym,
> > 
> > ```
> > 
> > lambda {|\*args| message\_received(sym, \*args)})  
> > end
> > 
> > The Proc yields \*args, which are then passed to message\_received. How  
> > can I get the Proc to yield a block that is passed in with the call to  
> > :sym? I tried this:
> > 
> > lambda {|\*args, &block| …
> > 
> > but it throws an error. Any other ideas? Let me know if the question  
> > is not clear.
> 
> You cannot currently do that with a block (1.9 and 2.0 will  
> allow &block arguments). In the meanwhile, you can #eval the  
> method into existence instead:
> 
> def define\_expected\_method(sym)  
> class \<\< self; self; end. **send** :class\_eval, %{  
> def #{sym}(\*args, &block)  
> message\_received sym, \*args, &block # ?  
> end  
> }  
> end

# One of these days… _sigh_

def define\_expected\_method(sym)  
class \<\< self; self; end. **send** :class\_eval, %{  
def #{sym}(\*args, &block)  
message\_received :#{sym}, \*args, &block # ?  
end  
}  
end

> > Thanks,  
> > David

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [September 3, 2006, 1:37am UTC](https://www.ruby-forum.com/t/yielding-a-block-to-another-block/67364/4 "2006-09-03T01:37:14Z")

</div>

David C. wrote:

> Thank you! Can I credit you when I commit this?

Certainly

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [September 2, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/yielding-a-block-to-another-block/67364/5 "2006-09-02T22:19:06Z")

</div>

Thank you! Can I credit you when I commit this?
