# How to use a class\_variable into a Module

**URL:** <https://www.ruby-forum.com/t/how-to-use-a-class-variable-into-a-module/138015>\
**Category:** Ruby\
**Created:** [May 19, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/how-to-use-a-class-variable-into-a-module/138015 "2008-05-19T14:56:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 19, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/how-to-use-a-class-variable-into-a-module/138015/1 "2008-05-19T14:56:50Z")

</div>

Hi, I want to declare a class\_variable in class A.  
Class A includes a Module M and I want some methods in M using the  
class\_variable but I get some problem:

* * *

module M  
def kk  
#puts (eval %{ #{send(‘class’).class\_variables[0]} })  
puts @@class\_var  
end  
end

class A  
include M  
@@class\_var = “kaka1”  
end

## a = A.new a.kk NameError: uninitialized class variable @@class\_var in M from ./class\_attr1.rb:4:in `kk’

I understand the problem. Because the nature of class variables when  
the Module M uses @@ it looks into a class variable defined in same  
class/module.

Is there any way I could use a class A @class\_var into a included M  
module methods? If not then I’ll use a normal attribute but I don’t  
like it since it’s a constant value that could perfectly be a class  
variable instead of initialiting it in each A instance creation.

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 19, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/how-to-use-a-class-variable-into-a-module/138015/2 "2008-05-19T15:49:04Z")

</div>

Hi,

IÃ±aki Baz C. wrote:

> Hi, I want to declare a class\_variable in class A.  
> Class A includes a Module M and I want some methods in M using the  
> class\_variable but I get some problem:
> 
> * * *
> 
> module M  
> def kk  
> #puts (eval %{ #{send(‘class’).class\_variables[0]} })  
> puts @@class\_var  
> end  
> end
> 
> class A  
> include M  
> @@class\_var = “kaka1”  
> end
> 
> ## a = A.new a.kk NameError: uninitialized class variable @@class\_var in M from ./class\_attr1.rb:4:in `kk’
> 
> I understand the problem. Because the nature of class variables when  
> the Module M uses @@ it looks into a class variable defined in same  
> class/module.
> 
> Is there any way I could use a class A @class\_var into a included M  
> module methods? If not then I’ll use a normal attribute but I don’t  
> like it since it’s a constant value that could perfectly be a class  
> variable instead of initialiting it in each A instance creation.

Here is a working code:

module M  
@@class\_var = nil  
def kk  
puts @@class\_var  
end  
end

class A  
include M  
@@class\_var = “kaka1”  
end

a = A.new  
a.kk

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 19, 2008, 5:12pm UTC](https://www.ruby-forum.com/t/how-to-use-a-class-variable-into-a-module/138015/3 "2008-05-19T17:12:14Z")

</div>

2008/5/19, Heesob P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> class A  
> include M  
> @@class\_var = “kaka1”  
> end
> 
> a = A.new  
> a.kk

It’s great!  
Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 19, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/how-to-use-a-class-variable-into-a-module/138015/4 "2008-05-19T17:17:38Z")

</div>

2008/5/19, Heesob P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> class A  
> include M  
> @@class\_var = “kaka1”  
> end

Hi, sorry but that is not valid (at least in my case). If you include  
M module in more clases, each one with its own @@class\_var, then  
@@class\_var will be shared between them that it’s not the expected  
behaviour:

* * *

module M  
@@class\_var = nil

def kk  
puts @@class\_var  
end  
end

class A  
include M  
@@class\_var = “@@ A”  
end

## class B include M @@class\_var = “@@ B” end

a=A.new  
a.kk  
=\> @@ B  
(it’s should be “@@ A” )
