# Module Eval Syntax

**URL:** <https://rubytalk.org/t/module-eval-syntax/24392>\
**Category:** ruby-talk\
**Created:** [26 January 2006 20:14 UTC](https://rubytalk.org/t/module-eval-syntax/24392 "2006-01-26T20:14:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Long2](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@John\_Long2](https://rubytalk.org/u/John_Long2)\
**Post date:** [26 January 2006 20:14 UTC](https://rubytalk.org/t/module-eval-syntax/24392/1 "2006-01-26T20:14:39Z")

</div>

> > class MyModule; end

=\> nil  
\>\> MyModule.class\_variables  
=\>   
\>\> MyModule.module\_eval do  
?\> @@test = true  
\>\> end  
=\> true  
\>\> @@test  
=\> true  
\>\> MyModule.module\_eval "@@weird = true"  
=\> true  
\>\> @@weird  
NameError: uninitialized class variable @@weird in Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):8  
\>\> MyModule.class\_variables  
=\> ["@@weird", "@@test"]  
\>\> Object.class\_variables  
=\> ["@@test"]

What in the world is going on here? Why does the block syntax eval differently than the string syntax? And why does @@test get defined on both MyModule and Object?

> **···**
>
> --  
> John Long  
> [http://wiseheartdesign.com](http://wiseheartdesign.com)

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford4](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Ross\_Bamford4](https://rubytalk.org/u/Ross_Bamford4)\
**Post date:** [26 January 2006 21:52 UTC](https://rubytalk.org/t/module-eval-syntax/24392/2 "2006-01-26T21:52:33Z")

</div>

This isn't a direct answer to John's question, but more an expansion of  
it that came up while I was playing with the original and my theory on  
what's going on. Considering this IRB:

&nbsp;&nbsp;module MyModule; end  
&nbsp;&nbsp;# =\> nil

&nbsp;&nbsp;MyModule.module\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;@@mod\_eval\_var = "mev"  
&nbsp;&nbsp;end  
&nbsp;&nbsp;# =\> "mev"

&nbsp;&nbsp;MyModule.instance\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;@@inst\_eval\_var = "iev"  
&nbsp;&nbsp;end  
&nbsp;&nbsp;# =\> "iev"

&nbsp;&nbsp;def MyModule.mkcv  
&nbsp;&nbsp;&nbsp;&nbsp;@@sing\_meth\_var = "smv"  
&nbsp;&nbsp;end  
&nbsp;&nbsp;# =\> nil

&nbsp;&nbsp;MyModule.mkcv  
&nbsp;&nbsp;# =\> "smv"

&nbsp;&nbsp;module MyModule  
&nbsp;&nbsp;&nbsp;&nbsp;def self.mkcv2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@cls\_sing\_meth\_var = "csmv"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;# =\> nil

&nbsp;&nbsp;MyModule.mkcv2  
&nbsp;&nbsp;# =\> "csmv"  
&nbsp;&nbsp;  
&nbsp;&nbsp;Object.class\_variables  
&nbsp;&nbsp;# =\> ["@@mod\_eval\_var", "@@inst\_eval\_var", "@@sing\_meth\_var"]  
&nbsp;&nbsp;Class.class\_variables  
&nbsp;&nbsp;# =\> ["@@mod\_eval\_var", "@@inst\_eval\_var", "@@sing\_meth\_var"]  
&nbsp;&nbsp;Module.class\_variables  
&nbsp;&nbsp;# =\> ["@@mod\_eval\_var", "@@inst\_eval\_var", "@@sing\_meth\_var"]  
&nbsp;&nbsp;MyModule.class\_variables  
&nbsp;&nbsp;# =\> ["@@cls\_sing\_meth\_var"]

Let me explain what I think I'm seeing here, and see if anyone can steer  
me right or expand on this behaviour. As far as I can tell, it seems  
that any time you make a @@class\_var outside of an actual class / module  
definition, you have to be extremely careful that you're actually  
defining where you think you are. I would guess that any time Ruby sees  
an @@ variable, it immediately goes to the enclosing class and defines  
the variable there. Maybe that explains the block behaviour, because  
although the blocks are evaluated in the module they somehow retain  
their original enclosing object's class - the top-level object in this  
case, of class Object.

The singleton method definition (def MyModule.mkcv) is actually creating  
a method on MyModule's singleton class, which is just an instance of  
Class. The new method becomes an 'instance method' of that singleton  
class, so that @@sing\_meth\_var = "smv" results in Class (the #class of  
the singleton Class instance) getting another class variable. This is  
demonstrated by:

&nbsp;&nbsp;sc = module MyModule  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self; self; end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;# =\> #\<Class:MyModule\>  
&nbsp;&nbsp;sc.class  
&nbsp;&nbsp;# =\> Class  
&nbsp;&nbsp;sc.class\_variables  
&nbsp;&nbsp;# =\> ["@@mod\_eval\_var", "@@inst\_eval\_var", "@@sing\_meth\_var"]

If this is true, then it explains why the class variables are present  
across Object, Class and Module's class variables - since all are  
instances of Class (and #class\_variables shows inherited vars). Since  
MyModule (an instance of class Module, a superclass of Class) doesn't  
see these variables, I have to assume they do end up defined on Class.  
More generally, once a class variable is defined on either Class or  
Object (each being the class of the other) it is to all intents and  
purposes defined everywhere, given the class\_variables behaviour I  
mentioned above.

The final mkcv2 definition, by virtue of being inside a reopened module  
definition, \*is\* defined as a a singleton method on MyModule, but either  
doesn't go to the singleton class (instead being actually redefined on  
the Module instance), or else is treated specially (which I somehow  
doubt but is possible). In any event, this ends up being defined where  
we'd expect it.

To further check out the line between a class and it's singleton class,  
I did a further experiment:

&nbsp;&nbsp;class SomeClazz  
&nbsp;&nbsp;&nbsp;&nbsp;@@icv = "icv"  
&nbsp;&nbsp;end  
&nbsp;&nbsp;# =\> "icv"

&nbsp;&nbsp;SomeClazz.class\_variables  
&nbsp;&nbsp;# =\> ["@@icv", "@@mod\_eval\_var", "@@inst\_eval\_var", "@@sing\_meth\_var"]

&nbsp;&nbsp;Class.class\_variables  
&nbsp;&nbsp;# =\> ["@@mod\_eval\_var", "@@inst\_eval\_var", "@@sing\_meth\_var"]

&nbsp;&nbsp;def SomeClazz.cval  
&nbsp;&nbsp;&nbsp;&nbsp;@@icv  
&nbsp;&nbsp;end  
&nbsp;&nbsp;# =\> nil

&nbsp;&nbsp;SomeClazz.cval  
&nbsp;&nbsp;NameError: uninitialized class variable @@icv in Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):90:in `cval'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):92

&nbsp;&nbsp;class SomeClazz  
&nbsp;&nbsp;&nbsp;&nbsp;def self.mcval  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@icv  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;# =\> nil

&nbsp;&nbsp;SomeClazz.mcval  
&nbsp;&nbsp;# =\> "icv"

This seems to confirm what I said above, and also (to me) is another  
great reason to stay away from class variables, especially when you're  
talking about class variables on Class or Module instances.

Anyway, this is just theory of course, and especially with the block stuff  
it seems pretty mysterious - maybe even "surprising" - but it would seem  
to fit I think - A string eval obviously doesn't close over it's scope so  
wouldn't be affected by it I guess. I hope someone can shed some more  
more light on this because it's going to bug me now 😉

> **···**
>
> from :0
> 
> --  
> Ross Bamford - [rosco@roscopeco.REMOVE.co.uk](mailto:rosco@roscopeco.REMOVE.co.uk)
