# Nuby question on Class Variable and Class Instance Variable

**URL:** <https://www.ruby-forum.com/t/nuby-question-on-class-variable-and-class-instance-variable/81867>\
**Category:** Ruby\
**Created:** [January 15, 2007, 9:17pm UTC](https://www.ruby-forum.com/t/nuby-question-on-class-variable-and-class-instance-variable/81867 "2007-01-15T21:17:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [January 15, 2007, 9:17pm UTC](https://www.ruby-forum.com/t/nuby-question-on-class-variable-and-class-instance-variable/81867/1 "2007-01-15T21:17:00Z")

</div>

class A  
@my\_var = 123  
@@my\_var = 234

def A.cv  
@@my\_var  
end

def A.cv=(val)  
@@my\_var = val  
end

class \<\<self  
attr\_accessor :my\_var  
end  
end

Obviously @my\_var and @@my\_var are two separate entitites, but are  
similar  
in many ways. I have some questions around them, any help is  
appreciated.

1. In terms of the figure in the PickAxe book 2nd Ed (page 381, lucille,  
Guitar etc), where would these two variables ( @my\_var and @@my\_var )  
appear?
2. If I have a subclass -  
class B \< A  
end  
the @@my\_var is shared between the parent and child but @my\_var is a  
separate instance in each of them. Are there any other such differences?
3. As I have written accessor for @my\_var, it most likely becomes a  
method  
in the virtual class A’, is there any way to write accessor for @@my\_var  
using Module’s attr\_accessor method?
4. As a general guideline where would I use one or the other?

Thanks  
Nasir

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [January 15, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/nuby-question-on-class-variable-and-class-instance-variable/81867/2 "2007-01-15T23:19:26Z")

</div>

On 1/15/07, Nasir K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @@my_var = val
> 
> ```
> 
> 1. In terms of the figure in the PickAxe book 2nd Ed (page 381, lucille,
> 2. As a general guideline where would I use one or the other?
> 
> Thanks  
> Nasir
> 
> Class variables are evil. A smarter man than me said something on the  
> subject of class variables on this list recently. So, hopefully that  
> answers number 4. I’ll leave it to the non noobs to answer the others.

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [January 16, 2007, 4:23am UTC](https://www.ruby-forum.com/t/nuby-question-on-class-variable-and-class-instance-variable/81867/3 "2007-01-16T04:23:01Z")

</div>

On 1/15/07, Nasir K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Obviously @my\_var and @@my\_var are two separate entitites, but are similar  
> in many ways. I have some questions around them, any help is appreciated.

Hi. This isn’t a direct answer to your question, but I wrote a little  
article around a recent discussion of this on the list.

> **[O'Reilly Media - Technology and Business Training](https://www.oreilly.com)**
>
> Gain technology and business knowledge and hone your skills with learning resources created and curated by O'Reilly's experts: live online training, video, books, our platform has content from 200+ of the world's best publishers.
