# Instance\_eval vs. class\_eval context

**URL:** <https://www.ruby-forum.com/t/instance-eval-vs-class-eval-context/102106>\
**Category:** Ruby\
**Created:** [August 10, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/instance-eval-vs-class-eval-context/102106 "2006-08-10T20:42:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![esquilax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@esquilax](https://www.ruby-forum.com/u/esquilax)\
**Post date:** [August 10, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/instance-eval-vs-class-eval-context/102106/1 "2006-08-10T20:42:35Z")

</div>

Is it the same context when a class is the receiver of instance\_eval or  
class\_eval/module\_eval?

---

<div class="post-metadata">

**Author:** ![esquilax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@esquilax](https://www.ruby-forum.com/u/esquilax)\
**Post date:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/instance-eval-vs-class-eval-context/102106/2 "2006-08-15T14:16:53Z")

</div>

On 8/10/06, Andrew G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is it the same context when a class is the receiver of instance\_eval or  
> class\_eval/module\_eval?

No. In case of instance\_eval you will be in the class, and in the  
case of (class|module)\_eval you will be in the class of the class  
(which is Class) [Hurray for multiple meanings to a word!]

---

<div class="post-metadata">

**Author:** ![esquilax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@esquilax](https://www.ruby-forum.com/u/esquilax)\
**Post date:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/instance-eval-vs-class-eval-context/102106/3 "2006-08-15T14:16:54Z")

</div>

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

A\> No. In case of instance\_eval you will be in the class, and in the  
A\> case of (class|module)\_eval you will be in the class of the class  
A\> (which is Class) [Hurray for multiple meanings to a word!]

no, not really. ruby use self and internally ruby\_class which give it  
where it can define method when it find keyword like ‘def’, ‘alias’,  
…

With obj.instance\_eval ruby will make

- self = obj , ruby\_class = obj singleton class

This mean that inside instance\_eval it will define singleton method

#instance\_eval work with any object, but a class is a little special  
because it can be seen as an object or as a class. This is why  
#module\_eval, #class\_eval exist.

With obj.class\_eval ruby will make

- self = obj, ruby\_class = obj

This mean that inside class\_eval ruby will define instance method.

One way to see it

moulon% ruby -e ‘class A; end; A.instance\_eval{ p self; def a() puts  
“A::a” end}; A.a’  
A  
A::a  
moulon%

moulon% ruby -e ‘class A; end; A.class\_eval{ p self; def a() puts “A#a”  
end}; A.new.a’  
A  
A#a  
moulon%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![esquilax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@esquilax](https://www.ruby-forum.com/u/esquilax)\
**Post date:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/instance-eval-vs-class-eval-context/102106/4 "2006-08-15T14:16:56Z")

</div>

On Aug 11, 2006, at 11:14 AM, ts wrote:

> With obj.instance\_eval ruby will make
> 
> - self = obj, ruby\_class = obj
> 
> moulon% ruby -e ‘class A; end; A.class\_eval{ p self; def a() puts  
> “A#a” end}; A.new.a’  
> A  
> A#a  
> moulon%

I’ve always had trouble understanding this and for some reason this  
is the email that finally clicked all the pieces into place for me.  
Thanks Guy!

James Edward G. II
