# Instance\_eval && class\_eval

**URL:** <https://rubytalk.org/t/instance-eval-class-eval/32152>\
**Category:** ruby-talk\
**Created:** [21 October 2006 10:55 UTC](https://rubytalk.org/t/instance-eval-class-eval/32152 "2006-10-21T10:55:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Artur\_Merke](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@Artur\_Merke](https://rubytalk.org/u/Artur_Merke)\
**Post date:** [21 October 2006 10:55 UTC](https://rubytalk.org/t/instance-eval-class-eval/32152/1 "2006-10-21T10:55:18Z")

</div>

Hi,

I'm just studing some metaprogramming and came across some problems.  
The following code:

> **···**
>
> ####################  
> class D  
> &nbsp;&nbsp;instance\_eval do  
> &nbsp;&nbsp;&nbsp;&nbsp;define\_method("i\_method") do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\nin i\_method"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;def ii\_method  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\nin ii\_method"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;class\_eval do  
> &nbsp;&nbsp;&nbsp;&nbsp;define\_method("c\_method") do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\nin c\_method"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;def cc\_method  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\nin cc\_method"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> D.new.i\_method  
> D.ii\_method  
> D.new.c\_method  
> D.new.cc\_method
> 
> D.i\_method  
> ##################
> 
> results in
> 
> in i\_method  
> in ii\_method  
> in c\_method  
> in cc\_method  
> metaruby2.rb:163: undefined method `i\_method' for D:Class (NoMethodE  
> rror)
> 
> so why is
> 
> D.i\_method wrong and  
> D.new.i\_method correct?
> 
> BTW, I know the right way of solving this, but I'm somehow missing  
> something which would explain it to me in a consitent manner
> 
> here the solution:  
> class D  
> &nbsp;&nbsp;(class \<\< self; self; end).instance\_eval do #class\_eval also does  
> the job  
> &nbsp;&nbsp;&nbsp;&nbsp;define\_method( :iii\_method ) do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\nin iii\_method"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> D.iii\_method
> 
> which works fine.
> 
> Artur

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [21 October 2006 11:11 UTC](https://rubytalk.org/t/instance-eval-class-eval/32152/2 "2006-10-21T11:11:43Z")

</div>

define\_method defines an instance method both in the instance\_eval and class\_eval sections of your code. To be able to call

&nbsp;&nbsp;&nbsp;D.method

method needs to be a class instance method of D (i.e. a method of the singleton class of D herself).

-- fxn

> **···**
>
> On Oct 21, 2006, at 12:55 PM, Artur Merke wrote:

---

<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:** [21 October 2006 13:35 UTC](https://rubytalk.org/t/instance-eval-class-eval/32152/3 "2006-10-21T13:35:17Z")

</div>

Artur Merke wrote:

> so why is  
> D.i\_method wrong and  
> D.new.i\_method correct?

I'm not surprised by that. I'm surprised that standard def...end syntax  
inside instance\_eval behaves differently from being in the class scope,  
when the self object in both cases is the same.

irb(main):001:0\> class Foo  
irb(main):002:1\> p "Defining aa in #{self}##{self.object\_id}"  
irb(main):003:1\> def aa; p :aa; end  
irb(main):004:1\> instance\_eval{  
irb(main):005:2\* p "Defining bb in #{self}##{self.object\_id}"  
irb(main):006:2\> def bb; p :bb; end  
irb(main):007:2\> }  
irb(main):008:1\> end  
"Defining aa in Foo#1699170"  
"Defining bb in Foo#1699170"  
=\> nil  
irb(main):009:0\> Foo.new.aa  
:aa  
=\> nil  
irb(main):010:0\> Foo.bb  
:bb  
=\> nil

---

<div class="post-metadata">

**Author:** ![Artur\_Merke](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@Artur\_Merke](https://rubytalk.org/u/Artur_Merke)\
**Post date:** [21 October 2006 12:45 UTC](https://rubytalk.org/t/instance-eval-class-eval/32152/4 "2006-10-21T12:45:15Z")

</div>

Xavier Noria schrieb:

> define\_method defines an instance method both in the instance\_eval  
> and class\_eval sections of your code. To be able to call
> 
> &nbsp;&nbsp;&nbsp;D.method
> 
> method needs to be a class instance method of D (i.e. a method of the  
> singleton class of D herself).

yes (see also my solution I provided at the end of my msg), but this  
was not the question,

the question was (and still is), why(!)

def a  
end

and

define\_method(:a) do  
end

behave differently in instance\_eval (and not in class\_eval and anywhere  
else) ...

AHA

Artur

> **···**
>
> > On Oct 21, 2006, at 12:55 PM, Artur Merke wrote:

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [21 October 2006 13:40 UTC](https://rubytalk.org/t/instance-eval-class-eval/32152/5 "2006-10-21T13:40:42Z")

</div>

> I'm not surprised by that. I'm surprised that standard def...end syntax  
> inside instance\_eval behaves differently from being in the class scope,  
> when the self object in both cases is the same.

def is a keyword and it don't use self to know where the method must be  
defined but it use an "internal" variable the "current" class.

> irb(main):001:0\> class Foo

here you have

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = current class = Foo

> irb(main):002:1\> p "Defining aa in #{self}##{self.object\_id}"  
> irb(main):003:1\> def aa; p :aa; end  
> irb(main):004:1\> instance\_eval{

in instance\_eval you have  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = Foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current class = Foo singleton class

> irb(main):005:2\* p "Defining bb in #{self}##{self.object\_id}"  
> irb(main):006:2\> def bb; p :bb; end

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [21 October 2006 13:36 UTC](https://rubytalk.org/t/instance-eval-class-eval/32152/6 "2006-10-21T13:36:35Z")

</div>

Ah yes, I see it now. I join the question, a posteriori looks like within instance\_eval we are in the context of the singleton class of D, because D.ii\_method works. But puts self does not seem to output that one there

&nbsp;&nbsp;&nbsp;irb(main):010:0\> class D; instance\_eval do; puts self; end; end  
&nbsp;&nbsp;&nbsp;D  
&nbsp;&nbsp;&nbsp;=\> nil  
&nbsp;&nbsp;&nbsp;irb(main):011:0\> class D; puts (class \<\< self; self; end); end  
&nbsp;&nbsp;&nbsp;#\<Class:D\>  
&nbsp;&nbsp;&nbsp;=\> nil

so I don't understand it either.

-- fxn

> **···**
>
> On Oct 21, 2006, at 2:45 PM, Artur Merke wrote:
> 
> > the question was (and still is), why(!)
> > 
> > def a  
> > end
> > 
> > and
> > 
> > define\_method(:a) do  
> > end
> > 
> > behave differently in instance\_eval (and not in class\_eval and anywhere  
> > else) ...

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [21 October 2006 15:20 UTC](https://rubytalk.org/t/instance-eval-class-eval/32152/7 "2006-10-21T15:20:44Z")

</div>

Good!

Is the existence of that "current" class vs self documented somewhere or do you know it from the sources? Does it have a name in jargon? What about the fact that instance\_eval switches current to the singleton class of self? I saw it works for objects which are not classes as well (which is a redundant exercise to reinforce this):

&nbsp;&nbsp;&nbsp;class A  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;a = A.new  
&nbsp;&nbsp;&nbsp;b = A.new

&nbsp;&nbsp;&nbsp;a.instance\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def croak  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "croak!"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;a.croak  
&nbsp;&nbsp;&nbsp;b.croak

gives

&nbsp;&nbsp;&nbsp;croak!  
&nbsp;&nbsp;&nbsp;tmp/foo.rb:13: undefined method `croak' for #\<A:0x1e6374\> (NoMethodError)

so the "current" class is the singleton class of the object a as well.

-- fxn

> **···**
>
> On Oct 21, 2006, at 3:40 PM, ts wrote:
> 
> > \> I'm not surprised by that. I'm surprised that standard def...end syntax  
> > \> inside instance\_eval behaves differently from being in the class scope,  
> > \> when the self object in both cases is the same.
> > 
> > def is a keyword and it don't use self to know where the method must be  
> > defined but it use an "internal" variable the "current" class.

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [21 October 2006 15:38 UTC](https://rubytalk.org/t/instance-eval-class-eval/32152/8 "2006-10-21T15:38:13Z")

</div>

> Is the existence of that "current" class vs self documented somewhere  
> or do you know it from the sources? Does it have a name in jargon?

ruby\_class : it's in the source, this mean that it's documented 🙂

> What about the fact that instance\_eval switches current to the  
> singleton class of self? I saw it works for objects which are not  
> classes as well (which is a redundant exercise to reinforce this):

Well a class can be seen as an object or as a class, i.e. with a class  
you can define a singleton method or a method.

#instance\_eval work with any object, and in this case a class is just an  
&nbsp;&nbsp;object like any other and def will define a singleton method

#class\_eval work only with classes (and modules) and in this case def will  
&nbsp;&nbsp;define instance methods.

Guy Decoux
