# Executing a block in the context of receiver

**URL:** <https://www.ruby-forum.com/t/executing-a-block-in-the-context-of-receiver/54812>\
**Category:** Ruby\
**Created:** [March 14, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/executing-a-block-in-the-context-of-receiver/54812 "2006-03-14T16:21:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [March 14, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/executing-a-block-in-the-context-of-receiver/54812/1 "2006-03-14T16:21:12Z")

</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) {  
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.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [March 14, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/executing-a-block-in-the-context-of-receiver/54812/2 "2006-03-14T16:42:32Z")

</div>

On Wed, 2006-03-15 at 00:20 +0900, Mark V. wrote:

> 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...

```

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

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [March 15, 2006, 9:40pm UTC](https://www.ruby-forum.com/t/executing-a-block-in-the-context-of-receiver/54812/3 "2006-03-15T21:40:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [March 15, 2006, 11:08pm UTC](https://www.ruby-forum.com/t/executing-a-block-in-the-context-of-receiver/54812/4 "2006-03-15T23:08:58Z")

</div>

On Thu, 2006-03-16 at 05:57 +0900, removed\_email\_address@domain.invalid wrote:

> > > }  
> > > instance\_eval &blk  
> > > # 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:

```
Object.new.instance_eval { |me| self == me }
# => 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.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [March 15, 2006, 9:58pm UTC](https://www.ruby-forum.com/t/executing-a-block-in-the-context-of-receiver/54812/5 "2006-03-15T21:58:26Z")

</div>

Hi –

On Wed, 15 Mar 2006, Ross B. wrote:

> > foo.do\_that(p3, p4)? I know I could just say “foo.do\_that(p3, p4)”,  
> > 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 🙂

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 (removed\_email\_address@domain.invalid)  
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:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [March 15, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/executing-a-block-in-the-context-of-receiver/54812/6 "2006-03-15T23:21:09Z")

</div>

Hi –

On Thu, 16 Mar 2006, Ross B. wrote:

> Probably irrelevant, but from what I can gather, instance\_eval will pass  
> the new self in as a block parameter, too:
> 
> Object.new.instance\_eval { |me| self == me }
> 
> # =\> 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 (removed\_email\_address@domain.invalid)  
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)
