# Using a @@class\_variable into a Module

**URL:** <https://www.ruby-forum.com/t/using-a-class-variable-into-a-module/138005>\
**Category:** Ruby\
**Created:** [May 19, 2008, 1:03pm UTC](https://www.ruby-forum.com/t/using-a-class-variable-into-a-module/138005 "2008-05-19T13:03:06Z")\
**Posts on this page:** 3\
**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, 1:03pm UTC](https://www.ruby-forum.com/t/using-a-class-variable-into-a-module/138005/1 "2008-05-19T13:03:06Z")

</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 20, 2008, 2:08am UTC](https://www.ruby-forum.com/t/using-a-class-variable-into-a-module/138005/2 "2008-05-20T02:08:05Z")

</div>

On Mon, May 19, 2008 at 8:02 PM, IÃ±aki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## end
> 
> 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.

So why not making it a constant? Try to stay away from class  
variables, they are evil.

---

<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 21, 2008, 4:44am UTC](https://www.ruby-forum.com/t/using-a-class-variable-into-a-module/138005/3 "2008-05-21T04:44:43Z")

</div>

2008/5/20, IÃ±aki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 2008/5/20, Michael F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > So why not making it a constant? Try to stay away from class  
> > variables, they are evil.
> 
> Nice suggestion, but I’m not sure if that wwill be valid since I need  
> various clases each one with a own constant value and all of then  
> including a module that uses that constant. I’ll try it.

Yeah, it works!

module M  
def kk  
puts self.class::VAR  
end  
end

class A  
include M  
VAR = “--------------------- @@ A ---------------------”  
end

class B  
include M  
VAR = “--------------------- @@ B ---------------------”  
end

irb\> a=A.new  
irb\> a.kk  
“--------------------- @@ A ---------------------”

irb\> b=B.new  
irb\> b.kk  
“--------------------- @@ BA ---------------------”

irb\> a.kk  
“--------------------- @@ A ---------------------”
