# \*\_eval

**URL:** <https://www.ruby-forum.com/t/eval/99429>\
**Category:** Ruby\
**Created:** [July 22, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/eval/99429 "2006-07-22T18:04:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [July 22, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/eval/99429/1 "2006-07-22T18:04:29Z")

</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  
def metaclass  
class \<\< self  
self  
end  
end  
end

class Foo  
block =  
lambda do  
p [self, object\_id]  
end

2.times do  
instance\_eval(&block)  
module\_eval(&block)  
class\_eval(&block)

```
 metaclass.instance_eval(&block)
 metaclass.module_eval(&block)
 metaclass.class_eval(&block)

```

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:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [July 22, 2006, 7:25pm UTC](https://www.ruby-forum.com/t/eval/99429/2 "2006-07-22T19:25:41Z")

</div>

> > > > > “E” == Erik V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

E\> There must be a difference. If I replace aModule.module\_eval  
E\> with aModule.instance\_eval in one spcific situation, the  
E\> 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:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [July 22, 2006, 7:45pm UTC](https://www.ruby-forum.com/t/eval/99429/3 "2006-07-22T19:45:41Z")

</div>

> 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  
instance\_eval \<\<-END  
p [self, object\_id]

```
 def bar1
 end

```

END

module\_eval \<\<-END  
p [self, object\_id]

```
 def bar2
 end

```

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:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [July 22, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/eval/99429/4 "2006-07-22T20:07:33Z")

</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  
instance\_eval \<\<-END  
p [self, object\_id]

```
 def bar1
 end

 define_method :bar2 do
 end

```

END

module\_eval \<\<-END  
p [self, object\_id]

```
 def bar3
 end

 define_method :bar4 do
 end

```

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:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [July 22, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/eval/99429/5 "2006-07-22T22:18:40Z")

</div>

Erik V. 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.

…

```
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

```
module M; end
mod = M # or some other module

```

I would use

```
m.module_eval {
	def foo; end
}

```

as a faster, cleaner way of accomplishing

```
eval <<-EOS
	module #{m.to_s}
		def foo; end
	end
EOS

```

which is hampered by its reliance on a string eval().  
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–

```
module Enumerable
	def func()
		any? whatever
		sort
		to_a
	end
end

enum.func()

```

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

```
func = proc do
	any? whatever
	sort
	to_a
end

```

and when you want somebody to call it, use

```
obj.instance_eval &func

```

The alternative would be

```
func = proc do |receiver|
	receiver.any? whatever
	receiver.sort
	receiver.to_a
end

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…

```
 Bye.
```

---

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [July 23, 2006, 1:50pm UTC](https://www.ruby-forum.com/t/eval/99429/6 "2006-07-23T13:50:45Z")

</div>

ts wrote:

> > > > > > “E” == Erik V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> E\> I never heard of this “current” before… Interesting… So,  
> E\> although the contexts look the same (self and object\_id), in  
> E\> 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.

---

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [July 23, 2006, 11:52am UTC](https://www.ruby-forum.com/t/eval/99429/7 "2006-07-23T11:52:24Z")

</div>

> > > > > “E” == Erik V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

E\> I never heard of this “current” before… Interesting… So,  
E\> although the contexts look the same (self and object\_id), in  
E\> 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
