# Ruby eval magic

**URL:** <https://www.ruby-forum.com/t/ruby-eval-magic/180186>\
**Category:** Ruby\
**Created:** [December 14, 2009, 10:46am UTC](https://www.ruby-forum.com/t/ruby-eval-magic/180186 "2009-12-14T10:46:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DMG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@DMG](https://www.ruby-forum.com/u/DMG)\
**Post date:** [December 14, 2009, 10:46am UTC](https://www.ruby-forum.com/t/ruby-eval-magic/180186/1 "2009-12-14T10:46:50Z")

</div>

Can anybody explain me, why this is true:  
self.class.class\_eval{self} == self.class.instance\_eval{self}

Thanks,  
Dawid

---

<div class="post-metadata">

**Author:** ![DMG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@DMG](https://www.ruby-forum.com/u/DMG)\
**Post date:** [December 14, 2009, 10:55am UTC](https://www.ruby-forum.com/t/ruby-eval-magic/180186/2 "2009-12-14T10:55:37Z")

</div>

On Dec 14, 10:33 am, DMG [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anybody explain me, why this is true:  
> self.class.class\_eval{self} == self.class.instance\_eval{self}
> 
> Thanks,  
> Dawid

It seems that we don’t need class\_eval method. Because if we use a  
Module or Class directly, we can use:  
SomeClass.instance\_eval { … }

and if we have an object and we need to work on its class, we can use:  
some\_object.class.instance\_eval { … }

So, do we need class\_eval/module\_eval?

Thanks,  
Dawid

---

<div class="post-metadata">

**Author:** ![DMG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@DMG](https://www.ruby-forum.com/u/DMG)\
**Post date:** [December 14, 2009, 11:51am UTC](https://www.ruby-forum.com/t/ruby-eval-magic/180186/3 "2009-12-14T11:51:06Z")

</div>

unknown wrote:

> It seems that we don’t need class\_eval method. Because if we use a  
> Module or Class directly, we can use:  
> SomeClass.instance\_eval { … }
> 
> and if we have an object and we need to work on its class, we can use:  
> some\_object.class.instance\_eval { … }
> 
> So, do we need class\_eval/module\_eval?

Yes, we do. But it took me a while until I found out why.

When Ruby code is executing, there are two bits of state which are  
maintained:

- the current object, which is exposed as ‘self’
- the current class, where instance methods are defined. This is pretty  
well hidden, but sometimes it matters.

Try the following code:

```
class Foo; end

Foo.class_eval "def bar; puts 'Hello!'; end"

Foo.new.bar

```

If you change class\_eval to instance\_eval, it won’t work as shown. In  
fact you’ll make a class method on Foo (Foo.bar)

AFAIK, class\_eval and module\_eval are the same thing though.

---

<div class="post-metadata">

**Author:** ![DMG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5e9695/32.png) [@DMG](https://www.ruby-forum.com/u/DMG)\
**Post date:** [December 14, 2009, 10:01pm UTC](https://www.ruby-forum.com/t/ruby-eval-magic/180186/4 "2009-12-14T22:01:41Z")

</div>

On Dec 14, 11:51 am, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> Foo.class_eval "def bar; puts 'Hello!'; end"
> 
> Foo.new.bar
> 
> ```
> 
> If you change class\_eval to instance\_eval, it won’t work as shown. In  
> fact you’ll make a class method on Foo (Foo.bar)

Oh, I see. Yes, the context matter when defining methods.  
Thank you very much!
