# Instance\_eval or class\_eval on metaclass?

**URL:** <https://www.ruby-forum.com/t/instance-eval-or-class-eval-on-metaclass/142800>\
**Category:** Ruby\
**Created:** [July 11, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/instance-eval-or-class-eval-on-metaclass/142800 "2008-07-11T17:23:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [July 11, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/instance-eval-or-class-eval-on-metaclass/142800/1 "2008-07-11T17:23:36Z")

</div>

I was looking at someone’s implementation of #meta\_eval and I noticed  
they used #instance\_eval. I’ve always been using #class\_eval with  
metaclasses and getting my desired effect. So why use #instance\_eval  
over #class\_eval when working with metaclasses?

Furthermore, I can’t even tell a difference, as demonstrated by this  
code snippet.

class Object  
def metaclass  
class \<\< self  
self  
end  
end  
end

class Test

def f  
metaclass.class\_eval do  
puts self.inspect  
end  
end

def g  
metaclass.instance\_eval do  
puts self.inspect  
end  
end

end

t = Test.new  
t.f  
t.g

#\<Class:#Test:0x8eae4\>  
#\<Class:#Test:0x8eae4\>

Using #class\_eval and #instance\_eval both produce the same result.  
Can anyone shed some light on this subject?

Thanks.

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [July 11, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/instance-eval-or-class-eval-on-metaclass/142800/2 "2008-07-11T17:33:32Z")

</div>

Hi –

On Sat, 12 Jul 2008, Christopher J. Bottaro wrote:

> class \<\< self  
> end  
> t = Test.new  
> t.f  
> t.g
> 
> #\<Class:#Test:0x8eae4\>  
> #\<Class:#Test:0x8eae4\>
> 
> Using #class\_eval and #instance\_eval both produce the same result.  
> Can anyone shed some light on this subject?

They give you the same self but they behave differently in the face of  
‘def’.

class Object  
def singleton\_class # Sorry; it’s what makes sense to me.  
class \<\< self  
self  
end  
end  
end

class TestMe

def f  
singleton\_class.class\_eval do  
puts self.inspect  
def x  
puts “def in class\_eval == instance method”  
end  
end  
end

def g  
singleton\_class.instance\_eval do  
puts self.inspect  
def y  
puts “def in obj. instance\_eval == singleton method on obj”  
end  
end  
end

end

t = TestMe.new  
t.f  
t.g  
t.x  
t.singleton\_class.y

You can see the same thing one level down too:

> > class C; end  
> > =\> nil
> 
> > C.class\_eval { def x; end }  
> > =\> nil
> 
> > C.instance\_eval { def y; end }  
> > =\> nil
> 
> > C.new.x  
> > =\> nil
> 
> > C.y  
> > =\> nil

David

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [July 15, 2008, 3:47am UTC](https://www.ruby-forum.com/t/instance-eval-or-class-eval-on-metaclass/142800/3 "2008-07-15T03:47:11Z")

</div>

Ahh, thanks for clearing that up for me.
