# Module variables

**URL:** <https://www.ruby-forum.com/t/module-variables/50063>\
**Category:** Ruby\
**Created:** [December 12, 2005, 7:43pm UTC](https://www.ruby-forum.com/t/module-variables/50063 "2005-12-12T19:43:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![akoSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@akoSS](https://www.ruby-forum.com/u/akoSS)\
**Post date:** [December 12, 2005, 7:43pm UTC](https://www.ruby-forum.com/t/module-variables/50063/1 "2005-12-12T19:43:14Z")

</div>

hello,

module MyModule  
def MyModule.mymethod(var)  
@var = var  
end  
end

1. when module method MyModule.mymethod creates module variable @var,  
and no class mixes in this module, where is this variable created?

2. module variables become class variables for the classes that mix in  
the module. so if the module method MyModule.mymethod changes module  
variable @var then this variable is changed in all the classes/objects  
that have this module mixed in. is this correct?

thanks  
konstantin

---

<div class="post-metadata">

**Author:** ![akoSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@akoSS](https://www.ruby-forum.com/u/akoSS)\
**Post date:** [December 12, 2005, 8:13pm UTC](https://www.ruby-forum.com/t/module-variables/50063/2 "2005-12-12T20:13:24Z")

</div>

On Dec 12, 2005, at 1:42 PM, ako… wrote:

> module MyModule  
> def MyModule.mymethod(var)  
> @var = var  
> end  
> end
> 
> 1. when module method MyModule.mymethod creates module variable @var,  
> and no class mixes in this module, where is this variable created?

The instance variable belongs to MyModule, which is an object. In  
particular, MyModule is an instance of the class Module. The instance  
variable is unaffected by any usage of MyModule via include/extend.

> 1. module variables become class variables for the classes that mix in  
> the module.

No. Instance variables are not propagated/shared via the class  
hierarchy  
nor via the mixin hierarchy.

> so if the module method MyModule.mymethod changes module  
> variable @var then this variable is changed in all the classes/objects  
> that have this module mixed in. is this correct?

Instance variables are private to the associated object, they are not  
shared.

Separate and distinct from instance variables are ‘class variables’  
which  
are shared via the class hierarchy. There are some subtle details of  
this  
sharing and in general you should view class variables and instance  
variables  
as two very different beasts.

Gary W.
