# Self in blocks

**URL:** <https://rubytalk.org/t/self-in-blocks/37511>\
**Category:** ruby-talk\
**Created:** [10 May 2007 21:37 UTC](https://rubytalk.org/t/self-in-blocks/37511 "2007-05-10T21:37:07Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vasco\_Andrade\_e\_Silv](https://avatars.discourse-cdn.com/v4/letter/v/43a26b/32.png) [@Vasco\_Andrade\_e\_Silv](https://rubytalk.org/u/Vasco_Andrade_e_Silv)\
**Post date:** [10 May 2007 21:37 UTC](https://rubytalk.org/t/self-in-blocks/37511/1 "2007-05-10T21:37:07Z")

</div>

Hi

This seems to me somehow inconsistent:

class A; end

# 1)  
# output A  
A.class\_eval { puts self }

# 2)  
# output main (in irb)  
[1].each { puts self }

The "problem" to me is that in 1) 'self' seems to refer to the context  
where yield is called, where in 2) seems to refer to the context in  
execution.  
What should i expect from 'self'`s behaviour?

Tks  
Vasco A. Silva

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [10 May 2007 21:41 UTC](https://rubytalk.org/t/self-in-blocks/37511/2 "2007-05-10T21:41:34Z")

</div>

self will normally refer to self in the lexical scope of the block (#2). class\_eval, module\_eval, instance\_eval, and instance\_exec are special exceptions to this rule.

-mental

> **···**
>
> On Fri, 11 May 2007 06:37:07 +0900, Vasco Andrade e silva \<vascoas@gmail.com\> wrote:
> 
> > The "problem" to me is that in 1) 'self' seems to refer to the context  
> > where yield is called, where in 2) seems to refer to the context in  
> > execution.  
> > What should i expect from 'self'`s behaviour?

---

<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:** [10 May 2007 21:45 UTC](https://rubytalk.org/t/self-in-blocks/37511/3 "2007-05-10T21:45:58Z")

</div>

Hi --

> Hi
> 
> This seems to me somehow inconsistent:
> 
> class A; end
> 
> # 1)  
> # output A  
> A.class\_eval { puts self }
> 
> # 2)  
> # output main (in irb)  
> [1].each { puts self }
> 
> The "problem" to me is that in 1) 'self' seems to refer to the context  
> where yield is called, where in 2) seems to refer to the context in  
> execution.  
> What should i expect from 'self'`s behaviour?

self doesn't really behave; it just plays the role it's assigned to  
play in different circumstances. class\_eval sets self to the  
receiver (it's specially designed for that purpose -- basically a  
programmatic rather than declarative way to enter a class definition  
block). In general, though, self doesn't change when you enter a  
code block. That's why self is still main inside your second block.

David

> **···**
>
> On Fri, 11 May 2007, Vasco Andrade e silva wrote:
> 
> --  
> Q. What is THE Ruby book for Rails developers?  
> A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> &nbsp;&nbsp;&nbsp;&nbsp;(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf\](http://www.rubypal.com/r4rrevs.pdf%5C))  
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [10 May 2007 21:48 UTC](https://rubytalk.org/t/self-in-blocks/37511/4 "2007-05-10T21:48:57Z")

</div>

One of the purposes of of #class\_eval is to explicitly change  
the binding of self within the block. By calling #class\_eval  
you are asking that self be bound to A (for your example) while  
the block is executing. This is a behavior of class\_eval and  
not of blocks in general, which is why in:

[1].each { puts self }

self isn't bound to the array.

Gary Wright

> **···**
>
> On May 10, 2007, at 5:37 PM, Vasco Andrade e silva wrote:
> 
> > # 1)  
> > # output A  
> > A.class\_eval { puts self }

---

<div class="post-metadata">

**Author:** ![Vasco\_Andrade\_e\_Silv](https://avatars.discourse-cdn.com/v4/letter/v/43a26b/32.png) [@Vasco\_Andrade\_e\_Silv](https://rubytalk.org/u/Vasco_Andrade_e_Silv)\
**Post date:** [10 May 2007 22:06 UTC](https://rubytalk.org/t/self-in-blocks/37511/5 "2007-05-10T22:06:29Z")

</div>

MenTaLguY wrote:

> self will normally refer to self in the lexical scope of the block (#2).  
> class\_eval, module\_eval, instance\_eval, and instance\_exec are special  
> exceptions to this rule.
> 
> -mental

I confess I don't like this approach :S (rules with exceptions, doesn't  
fit right..) however thanks for the explanations.  
By the way are class\_eval, module\_eval, instance\_eval and instance\_exec  
the only exceptions?

Vasco A. Silva

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<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:** [10 May 2007 22:11 UTC](https://rubytalk.org/t/self-in-blocks/37511/6 "2007-05-10T22:11:30Z")

</div>

Hi --

> MenTaLguY wrote:
> 
> > self will normally refer to self in the lexical scope of the block (#2).  
> > class\_eval, module\_eval, instance\_eval, and instance\_exec are special  
> > exceptions to this rule.
> > 
> > -mental
> 
> I confess I don't like this approach :S (rules with exceptions, doesn't  
> fit right..) however thanks for the explanations.  
> By the way are class\_eval, module\_eval, instance\_eval and instance\_exec  
> the only exceptions?

I think so. (I can't remember what instance\_exec does/will do, but  
that's neither here nor there.) It's not really rule plus exception;  
it's more a kind of hand-crafted quality to the language that provides  
a nice assortment of the facilities that people actually need, with  
attention paid to the naming. It's another example of why I have  
always said that Ruby represents "the triumph of balance over  
symmetry" 🙂

David

> **···**
>
> On Fri, 11 May 2007, Vasco Andrade e silva wrote:
> 
> --  
> Q. What is THE Ruby book for Rails developers?  
> A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> &nbsp;&nbsp;&nbsp;&nbsp;(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf\](http://www.rubypal.com/r4rrevs.pdf%5C))  
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [10 May 2007 22:21 UTC](https://rubytalk.org/t/self-in-blocks/37511/7 "2007-05-10T22:21:29Z")

</div>

In Ruby's core library, yes. It's possible to implement new methods with similar behavior (which is occasionally useful), but that isn't the norm.

-mental

> **···**
>
> On Fri, 11 May 2007 07:06:29 +0900, Vasco Andrade e silva \<vascoas@gmail.com\> wrote:
> 
> > By the way are class\_eval, module\_eval, instance\_eval and instance\_exec  
> > the only exceptions?

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [10 May 2007 22:55 UTC](https://rubytalk.org/t/self-in-blocks/37511/8 "2007-05-10T22:55:14Z")

</div>

For example:

class Person  
&nbsp;&nbsp;attr\_accessor :name, :age  
&nbsp;&nbsp;def initialize( name )  
&nbsp;&nbsp;&nbsp;&nbsp;@name = name  
&nbsp;&nbsp;&nbsp;&nbsp;@age = 0  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def grow\_older  
&nbsp;&nbsp;&nbsp;&nbsp;@age += 1  
&nbsp;&nbsp;end  
end

def create( name, &block )  
&nbsp;&nbsp;person = Person.new( name )  
&nbsp;&nbsp;person.instance\_eval( &block )  
&nbsp;&nbsp;p person  
end

create( :gavin ){  
&nbsp;&nbsp;@age = 32  
&nbsp;&nbsp;grow\_older  
}  
#=\> #\<Person:0x28347c4 @age=33, @name=:gavin\>

Using instance eval makes some DSLs (domain specific languages) easier  
to create, rather than having to use the object yielded to the block  
in every call. Compare the usage of 'create' above to this usage:

def create( name )  
&nbsp;&nbsp;person = Person.new( name )  
&nbsp;&nbsp;yield person  
&nbsp;&nbsp;p person  
end

create( :gavin ){ |person|  
&nbsp;&nbsp;person.age = 32  
&nbsp;&nbsp;person.grow\_older  
}  
#=\> #\<Person:0x28347c4 @age=33, @name=:gavin\>

> **···**
>
> On May 10, 4:21 pm, MenTaLguY \<men...@rydia.net\> wrote:
> 
> > On Fri, 11 May 2007 07:06:29 +0900, Vasco Andrade e silva \<vasc...@gmail.com\> wrote:
> > 
> > \> By the way are class\_eval, module\_eval, instance\_eval and instance\_exec  
> > \> the only exceptions?
> > 
> > In Ruby's core library, yes. It's possible to implement new methods with similar behavior  
> > (which is occasionally useful), but that isn't the norm.

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [11 May 2007 19:16 UTC](https://rubytalk.org/t/self-in-blocks/37511/9 "2007-05-11T19:16:16Z")

</div>

Don't forget define\_method .

> **···**
>
> On 5/10/07, MenTaLguY \<mental@rydia.net\> wrote:
> 
> > On Fri, 11 May 2007 07:06:29 +0900, Vasco Andrade e silva \<vascoas@gmail.com\> wrote:
> > 
> > \> By the way are class\_eval, module\_eval, instance\_eval and instance\_exec  
> > \> the only exceptions?
> > 
> > In Ruby's core library, yes. It's possible to implement new methods with similar behavior (which is occasionally useful), but that isn't the norm.
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Vasco\_Andrade\_e\_Silv](https://avatars.discourse-cdn.com/v4/letter/v/43a26b/32.png) [@Vasco\_Andrade\_e\_Silv](https://rubytalk.org/u/Vasco_Andrade_e_Silv)\
**Post date:** [17 May 2007 03:31 UTC](https://rubytalk.org/t/self-in-blocks/37511/10 "2007-05-17T03:31:30Z")

</div>

MenTaLguY wrote:

> In Ruby's core library, yes. It's possible to implement new methods  
> with similar behavior (which is occasionally useful), but that isn't the  
> norm.
> 
> -mental

How can i do that?

I was thinking.. If I want to "bind" self to yield context, with 0  
params, i can use a solution like this one:

> def create( name, &block )  
> person = Person.new( name )  
> person.instance\_eval( &block )  
> p person  
> end

person.instance\_eval( &block ) # some kind of yield(self) inside  
instance\_eval

but what if i want yield with params, and still have self like  
instance\_eval?

Example:  
class A  
&nbsp;&nbsp;def some\_method(&block)  
&nbsp;&nbsp;&nbsp;&nbsp;yield(:arg1, :arg2)  
&nbsp;&nbsp;end  
end  
a = A.new  
# wanted: self "equals" to a  
a.some\_method { |arg1, arg2| p self }

Vasco A. Silva

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [10 May 2007 23:00 UTC](https://rubytalk.org/t/self-in-blocks/37511/11 "2007-05-10T23:00:04Z")

</div>

Here's a real-world example of a DSL I created at work:

bp( 9 ){  
&nbsp;&nbsp;lopa( '02' ){  
&nbsp;&nbsp;&nbsp;&nbsp;sb "New Project Workflow.ppt"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cr 1331

&nbsp;&nbsp;&nbsp;&nbsp;sb "Add to Online Repository.ppt"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;crs 2216, 3560  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keywords "Workspace", "Phase 2"  
&nbsp;&nbsp;}  
}

For both blocks, the function creates an instance of a particular  
class and then uses instance\_eval to ensure that the methods  
referenced within that block are called on the appropriate object. If  
I had not mucked about with the self inside the blocks, I would have  
had to write something like:

bp( 9 ){ |blockpoint|  
&nbsp;&nbsp;blockpoint.lopa( '02' ){ |section|  
&nbsp;&nbsp;&nbsp;&nbsp;section.sb "New Project Workflow.ppt"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;section.cr 1331

&nbsp;&nbsp;&nbsp;&nbsp;section.sb "Add to Online Repository.ppt"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;section.crs 2216, 3560  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;section.keywords "Workspace", "Phase 2"  
&nbsp;&nbsp;}  
}

> **···**
>
> On May 10, 4:50 pm, Phrogz \<g...@refinery.com\> wrote:
> 
> > Using instance eval makes some DSLs (domain specific languages) easier  
> > to create, rather than having to use the object yielded to the block  
> > in every call.

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [17 May 2007 10:11 UTC](https://rubytalk.org/t/self-in-blocks/37511/12 "2007-05-17T10:11:12Z")

</div>

Now this is really \*ugly\*, but maybe you can make it less ugly?

class A  
&nbsp;&nbsp;def a &blk  
&nbsp;&nbsp;&nbsp;&nbsp;blk.call self, 42  
&nbsp;&nbsp;end  
end

A.new.a { |o,x| puts "#{self}: #{x}" }  
A.new.a { |o,x| o.instance\_eval{puts "#{self}: #{x}"} }

HTH  
Robert

> **···**
>
> On 5/17/07, Vasco Andrade e Silva \<vascoas@gmail.com\> wrote:
> 
> > MenTaLguY wrote:  
> > \> In Ruby's core library, yes. It's possible to implement new methods  
> > \> with similar behavior (which is occasionally useful), but that isn't the  
> > \> norm.  
> > \>  
> > \> -mental
> > 
> > How can i do that?
> > 
> > I was thinking.. If I want to "bind" self to yield context, with 0  
> > params, i can use a solution like this one:
> > 
> > \>def create( name, &block )  
> > \> person = Person.new( name )  
> > \> person.instance\_eval( &block )  
> > \> p person  
> > \>end
> > 
> > person.instance\_eval( &block ) # some kind of yield(self) inside  
> > instance\_eval
> > 
> > but what if i want yield with params, and still have self like  
> > instance\_eval?
> > 
> > Example:  
> > class A  
> > &nbsp;&nbsp;def some\_method(&block)  
> > &nbsp;&nbsp;&nbsp;&nbsp;yield(:arg1, :arg2)  
> > &nbsp;&nbsp;end  
> > end  
> > a = A.new  
> > # wanted: self "equals" to a  
> > a.some\_method { |arg1, arg2| p self }
> > 
> > Vasco A. Silva
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> You see things; and you say Why?  
> But I dream things that never were; and I say Why not?  
> -- George Bernard Shaw

---

<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:** [17 May 2007 12:06 UTC](https://rubytalk.org/t/self-in-blocks/37511/13 "2007-05-17T12:06:02Z")

</div>

Hi --

> MenTaLguY wrote:
> 
> > In Ruby's core library, yes. It's possible to implement new methods  
> > with similar behavior (which is occasionally useful), but that isn't the  
> > norm.
> > 
> > -mental
> 
> How can i do that?
> 
> I was thinking.. If I want to "bind" self to yield context, with 0  
> params, i can use a solution like this one:
> 
> > def create( name, &block )  
> > person = Person.new( name )  
> > person.instance\_eval( &block )  
> > p person  
> > end
> 
> person.instance\_eval( &block ) # some kind of yield(self) inside  
> instance\_eval
> 
> but what if i want yield with params, and still have self like  
> instance\_eval?
> 
> Example:  
> class A  
> def some\_method(&block)  
> &nbsp;&nbsp;&nbsp;yield(:arg1, :arg2)  
> end  
> end  
> a = A.new  
> # wanted: self "equals" to a  
> a.some\_method { |arg1, arg2| p self }

That's actually what instance\_exec does:

# Requires Ruby 1.9  
class A  
&nbsp;&nbsp;&nbsp;def x(&block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_exec(1,2,&block)  
&nbsp;&nbsp;&nbsp;end  
end

A.new.x {|a,b| p a, b, self }

# Output  
&nbsp;&nbsp;&nbsp;1  
&nbsp;&nbsp;&nbsp;2  
&nbsp;&nbsp;&nbsp;#\<A:0xb7f00700\>

I still don't know what the rationale is for the name, or how one is  
supposed to know that instance\_exec does it this way and instance\_eval  
does it the other way (except that I've been using instance\_eval for  
longer, but that won't help people who are just starting to learn  
Ruby).

David

> **···**
>
> On Thu, 17 May 2007, Vasco Andrade e Silva wrote:
> 
> --  
> Q. What is THE Ruby book for Rails developers?  
> A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> &nbsp;&nbsp;&nbsp;&nbsp;(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf\](http://www.rubypal.com/r4rrevs.pdf%5C))  
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [17 May 2007 22:41 UTC](https://rubytalk.org/t/self-in-blocks/37511/14 "2007-05-17T22:41:53Z")

</div>

> \>  
> \> \> By the way are class\_eval, module\_eval, instance\_eval and instance\_exec  
> \> \> the only exceptions?  
> \>  
> \> In Ruby's core library, yes. It's possible to implement new methods with similar behavior (which is occasionally useful), but that isn't the norm.
> 
> Don't forget define\_method .

You are a genius Rick!!!

So eventually I found what OP needs 😉

class A  
&nbsp;&nbsp;def a &blk  
&nbsp;&nbsp;&nbsp;&nbsp;i = 1  
&nbsp;&nbsp;&nbsp;&nbsp;m = methods  
&nbsp;&nbsp;&nbsp;&nbsp;i += 1 while methods.include? "x#{i}"  
&nbsp;&nbsp;&nbsp;&nbsp;self.class.send :define\_method, "x#{i}", &blk  
&nbsp;&nbsp;&nbsp;&nbsp;send "x#{i}", 42  
&nbsp;&nbsp;&nbsp;&nbsp;self.class.send :remove\_method, "x#{i}"  
&nbsp;&nbsp;end  
end

a = A.new  
a.a {|p| puts "#{self}: #{p}"}  
puts a.methods

Probably worth looking into Mauricios Eigenklass. He probably did it  
much better maybe caching the method (no idea how to avoid potential  
conflicts) ...

Cheers  
Robert

> **···**
>
> On 5/11/07, Rick DeNatale \<rick.denatale@gmail.com\> wrote:
> 
> > On 5/10/07, MenTaLguY \<mental@rydia.net\> wrote:  
> > \> On Fri, 11 May 2007 07:06:29 +0900, Vasco Andrade e silva \<vascoas@gmail.com\> wrote:
> > 
> > --  
> > Rick DeNatale
> > 
> > My blog on Ruby  
> > [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
> 
> --  
> You see things; and you say Why?  
> But I dream things that never were; and I say Why not?  
> -- George Bernard Shaw

---

<div class="post-metadata">

**Author:** ![Vasco\_Andrade\_e\_Silv](https://avatars.discourse-cdn.com/v4/letter/v/43a26b/32.png) [@Vasco\_Andrade\_e\_Silv](https://rubytalk.org/u/Vasco_Andrade_e_Silv)\
**Post date:** [11 May 2007 00:11 UTC](https://rubytalk.org/t/self-in-blocks/37511/15 "2007-05-11T00:11:43Z")

</div>

I agree with you (Gavin Kistner). The question is, what could you loose  
if self was always set as it is with class\_eval, instance\_eval, etc.?  
You could still do:

> def create( name )  
> person = Person.new( name )  
> yield person  
> p person  
> end
> 
> create( :gavin ){ |person|  
> person.age = 32  
> person.grow\_older  
> }

as all the other things.

For me the question is: What's the purpose of not always setting self to  
yield context?

Vasco A. Silva

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [17 May 2007 13:33 UTC](https://rubytalk.org/t/self-in-blocks/37511/16 "2007-05-17T13:33:20Z")

</div>

> \> I was thinking.. If I want to "bind" self to yield context, with 0  
> \> params, i can use a solution like this one:  
> \>  
> \>\> def create( name, &block )  
> \>\> person = Person.new( name )  
> \>\> person.instance\_eval( &block )  
> \>\> p person  
> \>\> end  
> \>  
> \> person.instance\_eval( &block ) # some kind of yield(self) inside  
> \> instance\_eval  
> \>  
> \> but what if i want yield with params, and still have self like

> That's actually what instance\_exec does:
> 
> # Requires Ruby 1.9  
> class A  
> &nbsp;&nbsp;&nbsp;def x(&block)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_exec(1,2,&block)  
> &nbsp;&nbsp;&nbsp;end  
> end

Or in Ruby 1.8.x you can use Mauricio's version:  
[http://eigenclass.org/hiki/bounded+space+instance\_exec](http://eigenclass.org/hiki/bounded+space+instance_exec)

> I still don't know what the rationale is for the name, or how one is  
> supposed to know that instance\_exec does it this way and instance\_eval  
> does it the other way (except that I've been using instance\_eval for  
> longer, but that won't help people who are just starting to learn  
> Ruby).

I'm not exactly sure where the name came from. Matz mentioned it in  
his RubyConf 2005 Keynote, but it seems that he got the idea, (and the  
name?) from someone posting on ruby-talk It only shows up as a  
one-line item on one of his last charts.  
[http://glu.ttono.us/articles/2005/10/16/matzs-keynotes](http://glu.ttono.us/articles/2005/10/16/matzs-keynotes)

\_why gave an implementation here:  
[http://redhanded.hobix.com/inspect/aBlockCostume.html](http://redhanded.hobix.com/inspect/aBlockCostume.html)

This seems a little more naive since it always uses the same name for  
the method :\_cloaker, and it's not thread-safe.

ActiveSupport in Rails defines Object#instance\_exec, but I'm not sure  
if this was the chicken or the egg. This implementation uses a similar  
technique of defining a method with the proc as the body, rails uses a  
timestamp to make the method name 'unique.'

Mauricio's first implementation was a bit more sophisticated in that  
it was thread-safe, but it didn't work with immediate value objects  
like FixNums.

Mauricio came up with a second version which worked with these, this  
version had a memory leak, leading to the latest version.

So that's the history of instance\_exec as far as I've been able to  
quickly ascertain.

Personally I'd prefer it if instance\_exec were used to do this so that

&nbsp;&nbsp;instance\_exec(1, 2) {|a, b| ....}

would pass 1 and 2 as the arguments to the block. Perhaps it was felt  
that this would lead to confusion with the current definition which  
either takes arguments (a string and optional file name and line  
number) OR a block, but not both.

> **···**
>
> On 5/17/07, dblack@wobblini.net \<dblack@wobblini.net\> wrote:
> 
> > On Thu, 17 May 2007, Vasco Andrade e Silva wrote:
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<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:** [11 May 2007 00:15 UTC](https://rubytalk.org/t/self-in-blocks/37511/17 "2007-05-11T00:15:05Z")

</div>

Hi --

> **···**
>
> On Fri, 11 May 2007, Vasco Andrade e silva wrote:
> 
> > I agree with you (Gavin Kistner). The question is, what could you loose  
> > if self was always set as it is with class\_eval, instance\_eval, etc.?  
> > You could still do:
> > 
> > > def create( name )  
> > > person = Person.new( name )  
> > > yield person  
> > > p person  
> > > end
> > > 
> > > create( :gavin ){ |person|  
> > > person.age = 32  
> > > person.grow\_older  
> > > }
> > 
> > as all the other things.
> > 
> > For me the question is: What's the purpose of not always setting self to  
> > yield context?
> 
> Why constrain it to be one way or the other, instead of being able to  
> do either? Besides, I want to be able to do this:
> 
> &nbsp;&nbsp;&nbsp;@x = 1  
> &nbsp;&nbsp;&nbsp;some\_method {|n| puts n + @x }
> 
> and not have it be some other object's @x.
> 
> David
> 
> --  
> Q. What is THE Ruby book for Rails developers?  
> A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> &nbsp;&nbsp;&nbsp;&nbsp;(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf\](http://www.rubypal.com/r4rrevs.pdf%5C))  
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Vasco\_Andrade\_e\_Silv](https://avatars.discourse-cdn.com/v4/letter/v/43a26b/32.png) [@Vasco\_Andrade\_e\_Silv](https://rubytalk.org/u/Vasco_Andrade_e_Silv)\
**Post date:** [11 May 2007 00:23 UTC](https://rubytalk.org/t/self-in-blocks/37511/18 "2007-05-11T00:23:46Z")

</div>

> Why constrain it to be one way or the other, instead of being able to  
> do either? Besides, I want to be able to do this:
> 
> &nbsp;&nbsp;@x = 1  
> &nbsp;&nbsp;some\_method {|n| puts n + @x }
> 
> and not have it be some other object's @x.
> 
> David

Excellent!! Your post said everything!  
I couldn't be more agree (now)! (I can see the light now 😉 )  
Thanks a lot!

Vasco A. Silva

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
