# Module\_eval is an instance method?

**URL:** <https://rubytalk.org/t/module-eval-is-an-instance-method/42501>\
**Category:** ruby-talk\
**Created:** [26 November 2007 04:55 UTC](https://rubytalk.org/t/module-eval-is-an-instance-method/42501 "2007-11-26T04:55:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [26 November 2007 04:55 UTC](https://rubytalk.org/t/module-eval-is-an-instance-method/42501/1 "2007-11-26T04:55:01Z")

</div>

pickaxe2 lists module\_eval as an instance method of Module. But:

num = 10  
puts Object.module\_eval('num')  
---\> 10

puts self  
--\>main

puts self.class  
--\>Object

puts self.module\_eval('num')  
undefined method `module\_eval' for main:Object (NoMethodError)

1) Why am I able to call module\_eval with a class name, i.e. Object?

2) Why can't I call module\_eval with an instance of Object?

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

---

<div class="post-metadata">

**Author:** ![Paul\_McMahon1](https://avatars.discourse-cdn.com/v4/letter/p/a88e4f/32.png) [@Paul\_McMahon1](https://rubytalk.org/u/Paul_McMahon1)\
**Post date:** [26 November 2007 05:10 UTC](https://rubytalk.org/t/module-eval-is-an-instance-method/42501/2 "2007-11-26T05:10:20Z")

</div>

module\_eval is a instance method of Module.

It is available to anything that is a Module.

irb(main):001:0\> Object.is\_a?(Module)  
=\> true  
irb(main):002:0\> String.is\_a?(Module)  
=\> true  
irb(main):003:0\> self.is\_a?(Module)  
=\> false

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [26 November 2007 05:19 UTC](https://rubytalk.org/t/module-eval-is-an-instance-method/42501/3 "2007-11-26T05:19:20Z")

</div>

Paul McMahon wrote:

> module\_eval is a instance method of Module.
> 
> It is available to anything that is a Module.

Thanks.

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

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [26 November 2007 05:43 UTC](https://rubytalk.org/t/module-eval-is-an-instance-method/42501/4 "2007-11-26T05:43:36Z")

</div>

Paul McMahon wrote:

> module\_eval is a instance method of Module.
> 
> It is available to anything that is a Module.
> 
> irb(main):001:0\> Object.is\_a?(Module)  
> =\> true  
> irb(main):002:0\> String.is\_a?(Module)  
> =\> true  
> irb(main):003:0\> self.is\_a?(Module)  
> =\> false

Ok, how do you explain this output:

def meth1  
&nbsp;&nbsp;Object.module\_eval('num')  
end

num = 10  
Object.module\_eval('num')  
--\>10

meth1  
--\>  
`module_eval': undefined local variable or method `num' for Object:Class  
(NameError)

According to pickaxe2, p. 393:

"When we define methods, we're actually creating (private) instance  
methods for class Object."

What about top level variables?

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

---

<div class="post-metadata">

**Author:** ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)\
**Post date:** [26 November 2007 06:20 UTC](https://rubytalk.org/t/module-eval-is-an-instance-method/42501/5 "2007-11-26T06:20:01Z")

</div>

> Ok, how do you explain this output:
> 
> def meth1  
> &nbsp;&nbsp;Object.module\_eval('num')  
> end
> 
> num = 10  
> Object.module\_eval('num')  
> --\>10
> 
> meth1  
> --\>  
> `module_eval': undefined local variable or method `num' for Object:Class  
> (NameError)

num is not in the scope of meth1 or of the Object class so resolution  
fails. If you use a global variable it works fine.

$num = 10  
def meth1  
&nbsp;&nbsp;Object.module\_eval('$num')  
end

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [26 November 2007 19:33 UTC](https://rubytalk.org/t/module-eval-is-an-instance-method/42501/6 "2007-11-26T19:33:16Z")

</div>

> Paul McMahon wrote:
> 
> > module\_eval is a instance method of Module.
> > 
> > It is available to anything that is a Module.
> > 
> > irb(main):001:0\> Object.is\_a?(Module)  
> > =\> true  
> > irb(main):002:0\> String.is\_a?(Module)  
> > =\> true  
> > irb(main):003:0\> self.is\_a?(Module)  
> > =\> false
> 
> Ok, how do you explain this output:
> 
> def meth1  
> &nbsp;&nbsp;Object.module\_eval('num')  
> end
> 
> num = 10  
> Object.module\_eval('num')  
> --\>10
> 
> meth1  
> --\>  
> `module_eval': undefined local variable or method `num' for Object:Class  
> (NameError)
> 
> According to pickaxe2, p. 393:
> 
> "When we define methods, we're actually creating (private) instance  
> methods for class Object."

Ruby pretty much says exactly what's going here: num is not a local variable or a method \*accessible inside the scope of meth1.\*

> What about top level variables?

I don't reccomend this, but you can get at them:

#!/usr/bin/env ruby -wKU

def print\_num  
&nbsp;&nbsp;&nbsp;puts eval("num", TOPLEVEL\_BINDING)  
end

num = 10  
print\_num

\_\_END\_\_

James Edward Gray II

> **···**
>
> On Nov 25, 2007, at 11:43 PM, 7stud -- wrote:

---

<div class="post-metadata">

**Author:** ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)\
**Post date:** [26 November 2007 06:25 UTC](https://rubytalk.org/t/module-eval-is-an-instance-method/42501/7 "2007-11-26T06:25:00Z")

</div>

Ps. You could also just use instance\_eval...

$num = 10  
def meth1  
&nbsp;&nbsp;instance\_eval('$num')  
end

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [26 November 2007 08:36 UTC](https://rubytalk.org/t/module-eval-is-an-instance-method/42501/8 "2007-11-26T08:36:52Z")

</div>

Jordan Callicoat wrote:

> > meth1  
> > --\>  
> > `module_eval': undefined local variable or method `num' for Object:Class  
> > (NameError)
> 
> num is not in the scope of meth1 or of the Object class so resolution  
> fails. If you use a global variable it works fine.

It's not in the scope of the Object class? Then why does this work:

num = 10  
Object.module\_eval('num')  
--\>10

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

---

<div class="post-metadata">

**Author:** ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)\
**Post date:** [26 November 2007 09:25 UTC](https://rubytalk.org/t/module-eval-is-an-instance-method/42501/9 "2007-11-26T09:25:00Z")

</div>

According to the method resolution order (MRO), ruby looks for the  
attribute in the local scope first, then in the containing scope which  
is exposed to the local scope. So in this case, num is the in local  
scope for Object#module\_eval, i.e., the local scope is self (which is  
"main"). If it's not in the local scope, then in this instance the  
exposed containing scope is the Object class. Since 'num' is not in  
the Object class, if you call Object.module\_eval('num') w/o defining  
num first, you get the same error. In other words, when you call  
Object#module\_eval from the top-level, the local scope is the top-  
level. But when you call it from inside a function, the local scope is  
the function. You can't access the higher scope of main from within  
the lower scope of the function. To do that you need to use a global  
variable, which exposes itself to all of the lower scopes of main.  
Hmmm...I'm probably not explaining that very well. Basically, anything  
you define in the top-level scope is only accessible from the top-  
level scope. For nested scopes you either have to pass the data as a  
parameter of the function, or use a global variable. Hope that makes  
some sense. I'm tired, so forgive me if I'm being incoherent!

Regards,  
Jordan

> **···**
>
> On Nov 26, 2:36 am, 7stud -- \<bbxx789\_0...@yahoo.com\> wrote:
> 
> > It's not in the scope of the Object class? Then why does this work:
> > 
> > num = 10  
> > Object.module\_eval('num')  
> > --\>10
