# Execute block in context of yielding method's module

**URL:** <https://rubytalk.org/t/execute-block-in-context-of-yielding-methods-module/22407>\
**Category:** ruby-talk\
**Created:** [12 November 2005 13:22 UTC](https://rubytalk.org/t/execute-block-in-context-of-yielding-methods-module/22407 "2005-11-12T13:22:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [12 November 2005 13:22 UTC](https://rubytalk.org/t/execute-block-in-context-of-yielding-methods-module/22407/1 "2005-11-12T13:22:14Z")

</div>

Mental block. Is there a reasonable way to do this?

&nbsp;&nbsp;def dothis ; puts "NO!" ; end

&nbsp;&nbsp;module M  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def dothis  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "THIS" + @number.to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def wrap( n, &yld )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;old = @number  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@number = n  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yld.call  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@number = old  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;M.wrap( 10 ) do  
&nbsp;&nbsp;&nbsp;&nbsp;dothis  
&nbsp;&nbsp;end

This produces "NO!". How do I get it to execute the block in the  
context of M, so that it produces "THIS10" (w/o having to out  
M.dothis).

Thanks,  
T.

---

<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:** [12 November 2005 13:32 UTC](https://rubytalk.org/t/execute-block-in-context-of-yielding-methods-module/22407/2 "2005-11-12T13:32:47Z")

</div>

instance\_eval is the standard way of changing 'self':

&nbsp;&nbsp;&nbsp;M.instance\_eval { wrap(10) { dothis } }

David

> **···**
>
> On Sat, 12 Nov 2005, Trans wrote:
> 
> > Mental block. Is there a reasonable way to do this?
> > 
> > def dothis ; puts "NO!" ; end
> > 
> > module M  
> > &nbsp;&nbsp;&nbsp;class \<\< self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def dothis  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "THIS" + @number.to\_s  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def wrap( n, &yld )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;old = @number  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@number = n  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yld.call  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@number = old  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > M.wrap( 10 ) do  
> > &nbsp;&nbsp;&nbsp;dothis  
> > end
> > 
> > This produces "NO!". How do I get it to execute the block in the  
> > context of M, so that it produces "THIS10" (w/o having to out  
> > M.dothis).
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [12 November 2005 14:42 UTC](https://rubytalk.org/t/execute-block-in-context-of-yielding-methods-module/22407/3 "2005-11-12T14:42:40Z")

</div>

Use  
&nbsp;&nbsp;instance\_eval &yld  
instead of  
&nbsp;&nbsp;yld.call  
?

Regards,

Sean

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [12 November 2005 23:32 UTC](https://rubytalk.org/t/execute-block-in-context-of-yielding-methods-module/22407/4 "2005-11-12T23:32:15Z")

</div>

Ah, right. Thanks.

T.

---

<div class="post-metadata">

**Author:** ![Christian\_Leskowsky](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@Christian\_Leskowsky](https://rubytalk.org/u/Christian_Leskowsky)\
**Post date:** [13 November 2005 00:57 UTC](https://rubytalk.org/t/execute-block-in-context-of-yielding-methods-module/22407/5 "2005-11-13T00:57:47Z")

</div>

Hey guys,

I'm very new to ruby.

This is some freaky shite! Could somebody give me a use case as to when I'd  
want to do something like this?

Thanks!

> **···**
>
> On 11/12/05, Trans \<transfire@gmail.com\> wrote:
> 
> > Ah, right. Thanks.
> > 
> > T.
> 
> --  
> -
> 
> Christian Leskowsky  
> christian.leskowsky@gmail.com

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [13 November 2005 02:58 UTC](https://rubytalk.org/t/execute-block-in-context-of-yielding-methods-module/22407/6 "2005-11-13T02:58:40Z")

</div>

harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;module Connection  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def host arg = nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@host = arg if arg  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@host  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def port arg = nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@port = arg if arg  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@port  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def configure &block  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval &block  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;Connection::configure do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host '[http://codeforpeople.com](http://codeforpeople.com)'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port 80  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;p Connection::host  
&nbsp;&nbsp;&nbsp;p Connection::port

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;"[http://codeforpeople.com](http://codeforpeople.com)"  
&nbsp;&nbsp;&nbsp;80

regards.

-a

> **···**
>
> On Sun, 13 Nov 2005, Christian Leskowsky wrote:
> 
> > Hey guys,
> > 
> > I'm very new to ruby.
> > 
> > This is some freaky shite! Could somebody give me a use case as to when I'd  
> > want to do something like this?
> > 
> > Thanks!
> 
> # --
> 
> > ara [dot] t [dot] howard [at] gmail [dot] com  
> > all happiness comes from the desire for others to be happy. all misery  
> > comes from the desire for oneself to be happy.  
> > -- bodhicaryavatara
> 
> ===============================================================================

---

<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:** [13 November 2005 14:50 UTC](https://rubytalk.org/t/execute-block-in-context-of-yielding-methods-module/22407/7 "2005-11-13T14:50:54Z")

</div>

Hi --

> Hey guys,
> 
> I'm very new to ruby.
> 
> This is some freaky shite! Could somebody give me a use case as to when I'd  
> want to do something like this?

Somewhat rarely 🙂 I actually prefer to know what 'self' is when I  
write a block. I'm uneasy with the idea that here:

&nbsp;&nbsp;&nbsp;some\_method { another\_method }

I can't tell by looking who's receiving the another\_method message.

A more common idiom is to provide the object so that you can see  
exactly who's doing what:

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :x  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;C.new do |c|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.x = 1  
&nbsp;&nbsp;&nbsp;end

and things like that.

David

> **···**
>
> On Sun, 13 Nov 2005, Christian Leskowsky wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Christian\_Leskowsky](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@Christian\_Leskowsky](https://rubytalk.org/u/Christian_Leskowsky)\
**Post date:** [13 November 2005 03:28 UTC](https://rubytalk.org/t/execute-block-in-context-of-yielding-methods-module/22407/8 "2005-11-13T03:28:04Z")

</div>

Interesting... Thanks!

> **···**
>
> On 11/12/05, Ara.T.Howard \<ara.t.howard@noaa.gov\> wrote:
> 
> > On Sun, 13 Nov 2005, Christian Leskowsky wrote:
> > 
> > \> Hey guys,  
> > \>  
> > \> I'm very new to ruby.  
> > \>  
> > \> This is some freaky shite! Could somebody give me a use case as to when  
> > I'd  
> > \> want to do something like this?  
> > \>  
> > \> Thanks!
> > 
> > harp:~ \> cat a.rb  
> > module Connection  
> > class \<\< self  
> > def host arg = nil  
> > @host = arg if arg  
> > @host  
> > end  
> > def port arg = nil  
> > @port = arg if arg  
> > @port  
> > end  
> > def configure &block  
> > instance\_eval &block  
> > end  
> > end  
> > end
> > 
> > Connection::configure do  
> > host '[http://codeforpeople.com](http://codeforpeople.com)'  
> > port 80  
> > end
> > 
> > p Connection::host  
> > p Connection::port
> > 
> > harp:~ \> ruby a.rb  
> > "[http://codeforpeople.com](http://codeforpeople.com)"  
> > 80
> > 
> > regards.
> > 
> > -a  
> > --
> > 
> > ===============================================================================  
> > \> ara [dot] t [dot] howard [at] gmail [dot] com  
> > \> all happiness comes from the desire for others to be happy. all misery  
> > \> comes from the desire for oneself to be happy.  
> > \> -- bodhicaryavatara
> > 
> > ===============================================================================
> 
> --  
> -
> 
> 'There was an owl lived in an oak.  
> The more he heard, the less he spoke.  
> The less he spoke, the more he heard.'
> 
> Christian Leskowsky  
> christian.leskowsky@gmail.com

---

<div class="post-metadata">

**Author:** ![Christian\_Leskowsky](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@Christian\_Leskowsky](https://rubytalk.org/u/Christian_Leskowsky)\
**Post date:** [13 November 2005 17:03 UTC](https://rubytalk.org/t/execute-block-in-context-of-yielding-methods-module/22407/9 "2005-11-13T17:03:50Z")

</div>

Thanks for the reply guys!

This last one looks much better to my eyes, but I'm still having problems.

I like the code I write to tell as much to the reader about what's going on  
as it possibly can-right there (without having to jump to other methods, in  
other classes, in other modules).

I think I'd probably have a hard time figuring out what:

C.new do |c|  
c.x = 1  
end

did, without having:

class C  
attr\_accessor :x  
def initialize  
yield self  
end  
end

being directly above it (on the same screen somewhere). It might take a few  
more keystrokes to write:

c = C.new  
c.x = 1

in general, but to me, it's much more readable.

The other thing that bugs me (and it's probably just an unfortunate  
example), is that it looks like I'm passing out a reference to myself,  
before I'm completely initialized. ugh!

Strange stuff... I think I'll just stay about as for away from it as I can  
:).

> **···**
>
> --  
> -
> 
> 'There was an owl lived in an oak.  
> The more he heard, the less he spoke.  
> The less he spoke, the more he heard.'
> 
> Christian Leskowsky  
> christian.leskowsky@gmail.com
> 
> On 11/13/05, David A. Black \<dblack@wobblini.net\> wrote:
> 
> > Hi --
> > 
> > On Sun, 13 Nov 2005, Christian Leskowsky wrote:
> > 
> > \> Hey guys,  
> > \>  
> > \> I'm very new to ruby.  
> > \>  
> > \> This is some freaky shite! Could somebody give me a use case as to when  
> > I'd  
> > \> want to do something like this?
> > 
> > Somewhat rarely 🙂 I actually prefer to know what 'self' is when I  
> > write a block. I'm uneasy with the idea that here:
> > 
> > some\_method { another\_method }
> > 
> > I can't tell by looking who's receiving the another\_method message.
> > 
> > A more common idiom is to provide the object so that you can see  
> > exactly who's doing what:
> > 
> > class C  
> > attr\_accessor :x  
> > def initialize  
> > yield self  
> > end  
> > end
> > 
> > C.new do |c|  
> > c.x = 1  
> > end
> > 
> > and things like that.
> > 
> > David
> > 
> > --  
> > David A. Black  
> > dblack@wobblini.net

---

<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:** [13 November 2005 17:13 UTC](https://rubytalk.org/t/execute-block-in-context-of-yielding-methods-module/22407/10 "2005-11-13T17:13:04Z")

</div>

Hi --

> Thanks for the reply guys!
> 
> This last one looks much better to my eyes, but I'm still having problems.
> 
> I like the code I write to tell as much to the reader about what's going on  
> as it possibly can-right there (without having to jump to other methods, in  
> other classes, in other modules).
> 
> I think I'd probably have a hard time figuring out what:
> 
> C.new do |c|  
> c.x = 1  
> end
> 
> did, without having:
> 
> class C  
> attr\_accessor :x  
> def initialize  
> yield self  
> end
> 
> being directly above it (on the same screen somewhere). It might take a few  
> more keystrokes to write:
> 
> c = C.new  
> c.x = 1
> 
> in general, but to me, it's much more readable.

Actually I just showed the construct without assignment. In full  
you'd want to assign it:

&nbsp;&nbsp;&nbsp;mc = MyClass.new do |m|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.x = 1  
&nbsp;&nbsp;&nbsp;end

(Easier to choose names with a multi-letter class name 🙂

Or if c existed already:

&nbsp;&nbsp;&nbsp;c = nil  
&nbsp;&nbsp;&nbsp;C.new do |c| etc.

because the block params pick up any existing variables and use those.  
(This may change circa 2.0.)

Also, most of the time you see this, there's more than one assignment  
going on in the block, so it has a nice encapsulating feel.

Mind you, all of this would have to go hand-in-hand with  
documentation. You probably wouldn't have the method code visible,  
but you'd have to know that MyClass.new yielded the new instance.

David

> **···**
>
> On Mon, 14 Nov 2005, Christian Leskowsky wrote:
> 
> > The other thing that bugs me (and it's probably just an unfortunate  
> > example), is that it looks like I'm passing out a reference to myself,  
> > before I'm completely initialized. ugh!
> > 
> > Strange stuff... I think I'll just stay about as for away from it as I can  
> > :).
> > 
> > --  
> > -
> > 
> > 'There was an owl lived in an oak.  
> > The more he heard, the less he spoke.  
> > The less he spoke, the more he heard.'
> > 
> > Christian Leskowsky  
> > christian.leskowsky@gmail.com
> > 
> > On 11/13/05, David A. Black \<dblack@wobblini.net\> wrote:
> > 
> > > Hi --
> > > 
> > > On Sun, 13 Nov 2005, Christian Leskowsky wrote:
> > > 
> > > > Hey guys,
> > > > 
> > > > I'm very new to ruby.
> > > > 
> > > > This is some freaky shite! Could somebody give me a use case as to when
> > > 
> > > I'd
> > > 
> > > > want to do something like this?
> > > 
> > > Somewhat rarely 🙂 I actually prefer to know what 'self' is when I  
> > > write a block. I'm uneasy with the idea that here:
> > > 
> > > some\_method { another\_method }
> > > 
> > > I can't tell by looking who's receiving the another\_method message.
> > > 
> > > A more common idiom is to provide the object so that you can see  
> > > exactly who's doing what:
> > > 
> > > class C  
> > > attr\_accessor :x  
> > > def initialize  
> > > yield self  
> > > end
> > > 
> > > C.new do |c|  
> > > c.x = 1  
> > > end
> > > 
> > > and things like that.
> > > 
> > > David
> > > 
> > > --  
> > > David A. Black  
> > > dblack@wobblini.net
> 
> --  
> David A. Black  
> dblack@wobblini.net
