# Instance\_eval && class\_eval

**URL:** <https://www.ruby-forum.com/t/instance-eval-class-eval/73102>\
**Category:** Ruby\
**Created:** [October 21, 2006, 12:56pm UTC](https://www.ruby-forum.com/t/instance-eval-class-eval/73102 "2006-10-21T12:56:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![seeweer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e5b9ba/32.png) [@seeweer](https://www.ruby-forum.com/u/seeweer)\
**Post date:** [October 21, 2006, 12:56pm UTC](https://www.ruby-forum.com/t/instance-eval-class-eval/73102/1 "2006-10-21T12:56:49Z")

</div>

Hi,

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

####################  
class D  
instance\_eval do  
define\_method(“i\_method”) do  
print “\nin i\_method”  
end  
def ii\_method  
print “\nin ii\_method”  
end  
end  
class\_eval do  
define\_method(“c\_method”) do  
print “\nin c\_method”  
end  
def cc\_method  
print “\nin cc\_method”  
end  
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  
(class \<\< self; self; end).instance\_eval do #class\_eval also does  
the job  
define\_method( :iii\_method ) do  
print “\nin iii\_method”  
end  
end  
end

D.iii\_method

which works fine.

Artur

---

<div class="post-metadata">

**Author:** ![seeweer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e5b9ba/32.png) [@seeweer](https://www.ruby-forum.com/u/seeweer)\
**Post date:** [October 21, 2006, 1:12pm UTC](https://www.ruby-forum.com/t/instance-eval-class-eval/73102/2 "2006-10-21T13:12:57Z")

</div>

On Oct 21, 2006, at 12:55 PM, Artur M. wrote:

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

D.method

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

– fxn

---

<div class="post-metadata">

**Author:** ![seeweer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e5b9ba/32.png) [@seeweer](https://www.ruby-forum.com/u/seeweer)\
**Post date:** [October 21, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/instance-eval-class-eval/73102/3 "2006-10-21T14:47:24Z")

</div>

Xavier N. schrieb:

> On Oct 21, 2006, at 12:55 PM, Artur M. wrote:
> 
> define\_method defines an instance method both in the instance\_eval  
> and class\_eval sections of your code. To be able to call
> 
> 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

---

<div class="post-metadata">

**Author:** ![seeweer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e5b9ba/32.png) [@seeweer](https://www.ruby-forum.com/u/seeweer)\
**Post date:** [October 21, 2006, 3:37pm UTC](https://www.ruby-forum.com/t/instance-eval-class-eval/73102/4 "2006-10-21T15:37:30Z")

</div>

On Oct 21, 2006, at 2:45 PM, Artur M. wrote:

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

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

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

so I don’t understand it either.

– fxn

---

<div class="post-metadata">

**Author:** ![seeweer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e5b9ba/32.png) [@seeweer](https://www.ruby-forum.com/u/seeweer)\
**Post date:** [October 21, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/instance-eval-class-eval/73102/5 "2006-10-21T15:35:30Z")

</div>

Artur M. 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:** ![seeweer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e5b9ba/32.png) [@seeweer](https://www.ruby-forum.com/u/seeweer)\
**Post date:** [October 21, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/instance-eval-class-eval/73102/6 "2006-10-21T15:41:31Z")

</div>

> > > > > “P” == Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

P\> I’m not surprised by that. I’m surprised that standard def…end  
syntax  
P\> inside instance\_eval behaves differently from being in the class  
scope,  
P\> 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.

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

here you have

```
                self = current class = Foo

```

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

in instance\_eval you have

```
                  self = Foo
                  current class = Foo singleton class

```

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

Guy Decoux

---

<div class="post-metadata">

**Author:** ![seeweer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e5b9ba/32.png) [@seeweer](https://www.ruby-forum.com/u/seeweer)\
**Post date:** [October 21, 2006, 5:22pm UTC](https://www.ruby-forum.com/t/instance-eval-class-eval/73102/7 "2006-10-21T17:22:00Z")

</div>

On Oct 21, 2006, at 3:40 PM, ts wrote:

> must be  
> defined but it use an “internal” variable the “current” class.

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):

class A  
end

a = A.new  
b = A.new

a.instance\_eval do  
def croak  
puts “croak!”  
end  
end

a.croak  
b.croak

gives

croak!  
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

---

<div class="post-metadata">

**Author:** ![seeweer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e5b9ba/32.png) [@seeweer](https://www.ruby-forum.com/u/seeweer)\
**Post date:** [October 21, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/instance-eval-class-eval/73102/8 "2006-10-21T17:40:03Z")

</div>

> > > > > “X” == Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

X\> Is the existence of that “current” class vs self documented somewhere  
X\> 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 🙂

X\> What about the fact that instance\_eval switches current to the  
X\> singleton class of self? I saw it works for objects which are not  
X\> 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  
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  
define instance methods.

Guy Decoux
