# Executing a block in the context of receiver

**URL:** <https://rubytalk.org/t/executing-a-block-in-the-context-of-receiver/25748>\
**Category:** ruby-talk\
**Created:** [14 March 2006 15:20 UTC](https://rubytalk.org/t/executing-a-block-in-the-context-of-receiver/25748 "2006-03-14T15:20:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Volkmann1](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@Mark\_Volkmann1](https://rubytalk.org/u/Mark_Volkmann1)\
**Post date:** [14 March 2006 15:20 UTC](https://rubytalk.org/t/executing-a-block-in-the-context-of-receiver/25748/1 "2006-03-14T15:20:09Z")

</div>

In the following code the do\_this instance method of the foo object  
calls yield to run the block that is passed to it.

foo.do\_this(p1, p2) {  
&nbsp;&nbsp;do\_that(p3, p4)  
}

Is there any way to make it so that do\_that is executed in the context  
of the foo object, i.e. it acts as though it was invoked with  
foo.do\_that(p3, p4)? I know I could just say "foo.do\_that(p3, p4)",  
but I was wondering if I could avoid that.

> **···**
>
> --  
> R. Mark Volkmann  
> Object Computing, Inc.

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford4](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Ross\_Bamford4](https://rubytalk.org/u/Ross_Bamford4)\
**Post date:** [14 March 2006 15:40 UTC](https://rubytalk.org/t/executing-a-block-in-the-context-of-receiver/25748/2 "2006-03-14T15:40:48Z")

</div>

If you're writing 'foo' then you can do:

&nbsp;&nbsp;class Foo  
&nbsp;&nbsp;&nbsp;&nbsp;def do\_this(&blk)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval &blk  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def do\_that  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Doing that..."  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;f.do\_this { do\_that }  
&nbsp;&nbsp;# (prints) Doing that...

&nbsp;&nbsp;# can use this form, too  
&nbsp;&nbsp;f.do\_this { |foo| foo.do\_that }  
&nbsp;&nbsp;# (prints) Doing that...

There's a few subtleties to using instance\_eval you might want to look  
up, but maybe it does the job...

> **···**
>
> On Wed, 2006-03-15 at 00:20 +0900, Mark Volkmann wrote:
> 
> > In the following code the do\_this instance method of the foo object  
> > calls yield to run the block that is passed to it.
> > 
> > foo.do\_this(p1, p2) {  
> > &nbsp;&nbsp;do\_that(p3, p4)  
> > }
> > 
> > Is there any way to make it so that do\_that is executed in the context  
> > of the foo object, i.e. it acts as though it was invoked with  
> > foo.do\_that(p3, p4)? I know I could just say "foo.do\_that(p3, p4)",  
> > but I was wondering if I could avoid that.
> 
> --  
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk

---

<div class="post-metadata">

**Author:** ![Eastcoastcoder](https://avatars.discourse-cdn.com/v4/letter/e/c0e974/32.png) [@Eastcoastcoder](https://rubytalk.org/u/Eastcoastcoder)\
**Post date:** [15 March 2006 20:38 UTC](https://rubytalk.org/t/executing-a-block-in-the-context-of-receiver/25748/3 "2006-03-15T20:38:47Z")

</div>

Also,  
send(:do\_that, p3, p4)

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [15 March 2006 20:57 UTC](https://rubytalk.org/t/executing-a-block-in-the-context-of-receiver/25748/4 "2006-03-15T20:57:36Z")

</div>

Hi --

> **···**
>
> On Wed, 15 Mar 2006, Ross Bamford wrote:
> 
> > On Wed, 2006-03-15 at 00:20 +0900, Mark Volkmann wrote:
> > 
> > > In the following code the do\_this instance method of the foo object  
> > > calls yield to run the block that is passed to it.
> > > 
> > > foo.do\_this(p1, p2) {  
> > > &nbsp;&nbsp;do\_that(p3, p4)  
> > > }
> > > 
> > > Is there any way to make it so that do\_that is executed in the context  
> > > of the foo object, i.e. it acts as though it was invoked with  
> > > foo.do\_that(p3, p4)? I know I could just say "foo.do\_that(p3, p4)",  
> > > but I was wondering if I could avoid that.
> > 
> > If you're writing 'foo' then you can do:
> > 
> > &nbsp;&nbsp;class Foo  
> > &nbsp;&nbsp;&nbsp;&nbsp;def do\_this(&blk)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval &blk  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def do\_that  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Doing that..."  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;f.do\_this { do\_that }  
> > &nbsp;&nbsp;# (prints) Doing that...
> > 
> > &nbsp;&nbsp;# can use this form, too  
> > &nbsp;&nbsp;f.do\_this { |foo| foo.do\_that }  
> > &nbsp;&nbsp;# (prints) Doing that...
> 
> But only if you yield self from do\_this (just to clarify). And  
> initialize f, but I guess that was implied 🙂
> 
> I like the second form better. Just having self change mysteriously  
> for the duration of a block always strikes me as a bit of an  
> obfuscation.
> 
> David
> 
> --  
> David A. Black (dblack@wobblini.net)  
> Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))
> 
> "Ruby for Rails" chapters now available  
> from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford4](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Ross\_Bamford4](https://rubytalk.org/u/Ross_Bamford4)\
**Post date:** [15 March 2006 22:07 UTC](https://rubytalk.org/t/executing-a-block-in-the-context-of-receiver/25748/5 "2006-03-15T22:07:39Z")

</div>

> Hi --
> 
> \>\> In the following code the do\_this instance method of the foo object  
> \>\> calls yield to run the block that is passed to it.  
> \>\>  
> \>\> foo.do\_this(p1, p2) {  
> \>\> do\_that(p3, p4)  
> \>\> }  
> \>\>  
> \>\> Is there any way to make it so that do\_that is executed in the context  
> \>\> of the foo object, i.e. it acts as though it was invoked with  
> \>\> foo.do\_that(p3, p4)? I know I could just say "foo.do\_that(p3, p4)",  
> \>\> but I was wondering if I could avoid that.  
> \>  
> \> If you're writing 'foo' then you can do:  
> \>  
> \> class Foo  
> \> def do\_this(&blk)  
> \> instance\_eval &blk  
> \> end  
> \>  
> \> def do\_that  
> \> puts "Doing that..."  
> \> end  
> \> end  
> \>  
> \> f.do\_this { do\_that }  
> \> # (prints) Doing that...  
> \>  
> \> # can use this form, too  
> \> f.do\_this { |foo| foo.do\_that }  
> \> # (prints) Doing that...
> 
> But only if you yield self from do\_this (just to clarify). And  
> initialize f, but I guess that was implied 🙂

(Oops, overzealous cut/paste job 🙂 )

Probably irrelevant, but from what I can gather, instance\_eval will pass  
the new self in as a block parameter, too:

&nbsp;&nbsp;Object.new.instance\_eval { |me| self == me }  
&nbsp;&nbsp;# =\> true

Though this doesn't seem to be documented...?

> I like the second form better. Just having self change mysteriously  
> for the duration of a block always strikes me as a bit of an  
> obfuscation.

I like instance\_eval for those cool DSL style things, but usually I'm  
with you on that one.

> **···**
>
> On Thu, 2006-03-16 at 05:57 +0900, dblack@wobblini.net wrote:
> 
> > On Wed, 15 Mar 2006, Ross Bamford wrote:  
> > \> On Wed, 2006-03-15 at 00:20 +0900, Mark Volkmann wrote:
> 
> --  
> Ross Bamford - rosco@roscopeco.REMOVE.co.uk

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [15 March 2006 22:19 UTC](https://rubytalk.org/t/executing-a-block-in-the-context-of-receiver/25748/6 "2006-03-15T22:19:08Z")

</div>

Hi --

> **···**
>
> On Thu, 16 Mar 2006, Ross Bamford wrote:
> 
> > Probably irrelevant, but from what I can gather, instance\_eval will pass  
> > the new self in as a block parameter, too:
> > 
> > &nbsp;&nbsp;Object.new.instance\_eval { |me| self == me }  
> > &nbsp;&nbsp;# =\> true
> > 
> > Though this doesn't seem to be documented...?
> 
> Interesting -- definitely relevant, too, in the sense that my  
> correction was wrong. I don't remember seeing that documented either.
> 
> David
> 
> --  
> David A. Black (dblack@wobblini.net)  
> Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))
> 
> "Ruby for Rails" chapters now available  
> from Manning Early Access Program! [http://www.manning.com/books/black](http://www.manning.com/books/black)
