# Ruby eval magic

**URL:** <https://rubytalk.org/t/ruby-eval-magic/56627>\
**Category:** ruby-talk\
**Created:** [14 December 2009 09:42 UTC](https://rubytalk.org/t/ruby-eval-magic/56627 "2009-12-14T09:42:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DMG1](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@DMG1](https://rubytalk.org/u/DMG1)\
**Post date:** [14 December 2009 09:42 UTC](https://rubytalk.org/t/ruby-eval-magic/56627/1 "2009-12-14T09:42:48Z")

</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:** ![DMG1](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@DMG1](https://rubytalk.org/u/DMG1)\
**Post date:** [14 December 2009 09:55 UTC](https://rubytalk.org/t/ruby-eval-magic/56627/2 "2009-12-14T09:55:06Z")

</div>

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

> **···**
>
> On Dec 14, 10:33 am, DMG \<dmg2...@dmg.pl\> wrote:
> 
> > 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:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [14 December 2009 10:51 UTC](https://rubytalk.org/t/ruby-eval-magic/56627/3 "2009-12-14T10:51:13Z")

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

&nbsp;&nbsp;&nbsp;&nbsp;class Foo; end

&nbsp;&nbsp;&nbsp;&nbsp;Foo.class\_eval "def bar; puts 'Hello!'; end"

&nbsp;&nbsp;&nbsp;&nbsp;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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![DMG1](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@DMG1](https://rubytalk.org/u/DMG1)\
**Post date:** [14 December 2009 21:00 UTC](https://rubytalk.org/t/ruby-eval-magic/56627/4 "2009-12-14T21:00:08Z")

</div>

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

> **···**
>
> On Dec 14, 11:51 am, Brian Candler \<b.cand...@pobox.com\> wrote:
> 
> > 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 &quot;def bar; puts &#39;Hello\!&#39;; end&quot;
> > 
> > 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)
