# \*\_eval

**URL:** <https://rubytalk.org/t/eval/29193>\
**Category:** ruby-talk\
**Created:** [22 July 2006 16:01 UTC](https://rubytalk.org/t/eval/29193 "2006-07-22T16:01:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erik\_Veenstra1](https://avatars.discourse-cdn.com/v4/letter/e/f04885/32.png) [@Erik\_Veenstra1](https://rubytalk.org/u/Erik_Veenstra1)\
**Post date:** [22 July 2006 16:01 UTC](https://rubytalk.org/t/eval/29193/1 "2006-07-22T16:01:41Z")

</div>

Can somebody explain me, in detail, the differences between  
aModule.instance\_eval, aModule.module\_eval and  
aModule.class\_eval?

I'm especially interested in the differences in context.

There must be a difference. If I replace aModule.module\_eval  
with aModule.instance\_eval in one spcific situation, the  
results differ, although self.inspect returns the same value...

Thanks.

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

> **···**
>
> ----------------------------------------------------------------
> 
> class Object  
> &nbsp;&nbsp;&nbsp;def metaclass  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> class Foo  
> &nbsp;&nbsp;&nbsp;block =  
> &nbsp;&nbsp;&nbsp;lambda do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p [self, object\_id]  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;2.times do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval(&block)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module\_eval(&block)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class\_eval(&block)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;metaclass.instance\_eval(&block)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;metaclass.module\_eval(&block)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;metaclass.class\_eval(&block)  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> ----------------------------------------------------------------
> 
> [Foo, -605291388]  
> [Foo, -605291388]  
> [Foo, -605291388]  
> [#\<Class:Foo\> , -605291428]  
> [#\<Class:Foo\> , -605291428]  
> [#\<Class:Foo\> , -605291428]  
> [Foo, -605291388]  
> [Foo, -605291388]  
> [Foo, -605291388]  
> [#\<Class:Foo\> , -605291428]  
> [#\<Class:Foo\> , -605291428]  
> [#\<Class:Foo\> , -605291428]
> 
> ----------------------------------------------------------------

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [22 July 2006 17:24 UTC](https://rubytalk.org/t/eval/29193/2 "2006-07-22T17:24:09Z")

</div>

> There must be a difference. If I replace aModule.module\_eval  
> with aModule.instance\_eval in one spcific situation, the  
> results differ, although self.inspect returns the same value...

Well, when you use keywords (like `def' 'alias') ruby use the  
"current" class to know where it must define the method.

With #instance\_eval, the current class is the singleton class and ruby  
will define singleton methods

With #module\_eval, the current is the class (i.e. self) and ruby define  
instance methods.

By default, #class\_eval is the same than #module\_eval

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Dumaiu](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@Dumaiu](https://rubytalk.org/u/Dumaiu)\
**Post date:** [22 July 2006 20:15 UTC](https://rubytalk.org/t/eval/29193/3 "2006-07-22T20:15:08Z")

</div>

Erik Veenstra wrote:

> Can somebody explain me, in detail, the differences between  
> aModule.instance\_eval, aModule.module\_eval and  
> aModule.class\_eval?
> 
> I'm especially interested in the differences in context.

...

&nbsp;&nbsp;I think inspect() and object\_id() confuse the issue, and trying to use  
instance\_eval() at module scope is about as much fun as putting a  
screwdriver through your eye. My own views are as follows:  
module\_eval() is intended for defining methods dynamically, and its  
receiver should be of class Module. Given

&nbsp;&nbsp;module M; end  
&nbsp;&nbsp;mod = M # or some other module

I would use

&nbsp;&nbsp;m.module\_eval {  
&nbsp;&nbsp;&nbsp;&nbsp;def foo; end  
&nbsp;&nbsp;}

as a faster, cleaner way of accomplishing

&nbsp;&nbsp;eval \<\<-EOS  
&nbsp;&nbsp;&nbsp;&nbsp;module #{m.to\_s}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def foo; end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;EOS

which is hampered by its reliance on a string eval().  
&nbsp;&nbsp;On the other hand, I see instance\_eval() as primarily useful for  
\*calling\* a blockful of methods, with a dynamically-determined  
receiver. Say you have a bunch of stuff you want some object to  
do--e.g., you know it'll be Enumerable, so you have some Enumerable  
methods--

&nbsp;&nbsp;module Enumerable  
&nbsp;&nbsp;&nbsp;&nbsp;def func()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;any? whatever  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to\_a  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;enum.func()

but then you realize that you might want to use this more generally,  
not just on Enumerable objects. So you wrap it:

&nbsp;&nbsp;func = proc do  
&nbsp;&nbsp;&nbsp;&nbsp;any? whatever  
&nbsp;&nbsp;&nbsp;&nbsp;sort  
&nbsp;&nbsp;&nbsp;&nbsp;to\_a  
&nbsp;&nbsp;end

and when you want somebody to call it, use

&nbsp;&nbsp;obj.instance\_eval &func

The alternative would be

&nbsp;&nbsp;func = proc do |receiver|  
&nbsp;&nbsp;&nbsp;&nbsp;receiver.any? whatever  
&nbsp;&nbsp;&nbsp;&nbsp;receiver.sort  
&nbsp;&nbsp;&nbsp;&nbsp;receiver.to\_a  
&nbsp;&nbsp;end

&nbsp;&nbsp;func.call(obj)

which is a less elegant factorization.

...

> I'll switch back to good old Lisp...
> 
> Good bye, brave Ruby people...
> 
> It was a nice time...
> 
> So long...

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bye.

---

<div class="post-metadata">

**Author:** ![Erik\_Veenstra1](https://avatars.discourse-cdn.com/v4/letter/e/f04885/32.png) [@Erik\_Veenstra1](https://rubytalk.org/u/Erik_Veenstra1)\
**Post date:** [22 July 2006 17:42 UTC](https://rubytalk.org/t/eval/29193/4 "2006-07-22T17:42:17Z")

</div>

> \> There must be a difference. If I replace  
> \> aModule.module\_eval with aModule.instance\_eval in one  
> \> spcific situation, the results differ, although  
> \> self.inspect returns the same value...
> 
> Well, when you use keywords (like `def' 'alias') ruby use the  
> "current" class to know where it must define the method.
> 
> With #instance\_eval, the current class is the singleton class and ruby  
> will define singleton methods
> 
> With #module\_eval, the current is the class (i.e. self) and ruby define  
> instance methods.

I never heard of this "current" before... Interesting... So,  
although the contexts look the same (self and object\_id), in  
reality, they aren't...

POLS? Well, \_I\_ am surprised!

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

> **···**
>
> ----------------------------------------------------------------
> 
> class Foo  
> &nbsp;&nbsp;&nbsp;instance\_eval \<\<-END  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p [self, object\_id]
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def bar1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;END
> 
> &nbsp;&nbsp;&nbsp;module\_eval \<\<-END  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p [self, object\_id]
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def bar2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;END  
> end
> 
> p Foo.method\_defined?(:bar1)  
> p Foo.method\_defined?(:bar2)
> 
> p Foo.new.method(:bar1) rescue nil # No output!  
> p Foo.new.method(:bar2) rescue nil
> 
> ----------------------------------------------------------------
> 
> [Foo, -605293266]  
> [Foo, -605293266]  
> false  
> true  
> #\<Method: Foo#bar2\>
> 
> ----------------------------------------------------------------

---

<div class="post-metadata">

**Author:** ![Erik\_Veenstra1](https://avatars.discourse-cdn.com/v4/letter/e/f04885/32.png) [@Erik\_Veenstra1](https://rubytalk.org/u/Erik_Veenstra1)\
**Post date:** [22 July 2006 18:04 UTC](https://rubytalk.org/t/eval/29193/5 "2006-07-22T18:04:32Z")

</div>

> Well, when you use keywords (like `def' 'alias') ruby use the  
> \> "current" class to know where it must define the method.

It's really only true for keyword stuff. define\_method works as  
expected...

> POLS? Well, \_I\_ am surprised!

It's the first time, in all those years, that I'm surprised by  
Ruby. Well, I'm often surprised by Ruby, but that's in a  
positive way. Now I'm surprised in a negative way. I'm  
surprised being surprised! It's a shock!

I'll switch back to good old Lisp...

Good bye, brave Ruby people...

It was a nice time...

So long...

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

> **···**
>
> ----------------------------------------------------------------
> 
> class Foo  
> &nbsp;&nbsp;&nbsp;instance\_eval \<\<-END  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p [self, object\_id]
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def bar1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method :bar2 do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;END
> 
> &nbsp;&nbsp;&nbsp;module\_eval \<\<-END  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p [self, object\_id]
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def bar3  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method :bar4 do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;END  
> end
> 
> p Foo.method\_defined?(:bar1) # false  
> p Foo.method\_defined?(:bar2) # true  
> p Foo.method\_defined?(:bar3) # true  
> p Foo.method\_defined?(:bar4) # true
> 
> p Foo.new.method(:bar1) rescue nil # no output!  
> p Foo.new.method(:bar2) rescue nil # #\<Method: Foo#bar2\>  
> p Foo.new.method(:bar3) rescue nil # #\<Method: Foo#bar2\>  
> p Foo.new.method(:bar4) rescue nil # #\<Method: Foo#bar2\>
> 
> ----------------------------------------------------------------
> 
> [Foo, -605293706]  
> [Foo, -605293706]  
> false  
> true  
> true  
> true  
> #\<Method: Foo#bar2\>  
> #\<Method: Foo#bar3\>  
> #\<Method: Foo#bar4\>
> 
> ----------------------------------------------------------------

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [23 July 2006 09:51 UTC](https://rubytalk.org/t/eval/29193/6 "2006-07-23T09:51:19Z")

</div>

> I never heard of this "current" before... Interesting... So,  
> although the contexts look the same (self and object\_id), in  
> reality, they aren't...

Well, I hope that you have seen that at toplevel (where self is an  
\*instance\* of Object) ruby define instance method for Object.

When it's in the class A (where self is A) it define instance method for A

Guy Decoux

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [23 July 2006 11:50 UTC](https://rubytalk.org/t/eval/29193/7 "2006-07-23T11:50:12Z")

</div>

ts wrote:

> \> I never heard of this "current" before... Interesting... So,  
> \> although the contexts look the same (self and object\_id), in  
> \> reality, they aren't...
> 
> Well, I hope that you have seen that at toplevel (where self is an  
> \*instance\* of Object) ruby define instance method for Object.

I've often wondered why it wasn't an instance of Module with an extend  
self.

T.
