# Nuby question on Class Variable and Class Instance Variable

**URL:** <https://rubytalk.org/t/nuby-question-on-class-variable-and-class-instance-variable/34477>\
**Category:** ruby-talk\
**Created:** [15 January 2007 20:16 UTC](https://rubytalk.org/t/nuby-question-on-class-variable-and-class-instance-variable/34477 "2007-01-15T20:16:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nasir\_Khan](https://avatars.discourse-cdn.com/v4/letter/n/5daacb/32.png) [@Nasir\_Khan](https://rubytalk.org/u/Nasir_Khan)\
**Post date:** [15 January 2007 20:16 UTC](https://rubytalk.org/t/nuby-question-on-class-variable-and-class-instance-variable/34477/1 "2007-01-15T20:16:15Z")

</div>

class A  
&nbsp;&nbsp;@my\_var = 123  
&nbsp;&nbsp;@@my\_var = 234

&nbsp;&nbsp;def [A.cv](http://A.cv)  
&nbsp;&nbsp;&nbsp;@@my\_var  
&nbsp;&nbsp;end

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

&nbsp;&nbsp;class \<\<self  
&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :my\_var  
&nbsp;&nbsp;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 -  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class B \< A  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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:** ![Jason\_Mayer](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jason\_Mayer](https://rubytalk.org/u/Jason_Mayer)\
**Post date:** [15 January 2007 22:18 UTC](https://rubytalk.org/t/nuby-question-on-class-variable-and-class-instance-variable/34477/2 "2007-01-15T22:18:27Z")

</div>

> class A  
> &nbsp;&nbsp;@my\_var = 123  
> &nbsp;&nbsp;@@my\_var = 234
> 
> &nbsp;&nbsp;def A.cv  
> &nbsp;&nbsp;&nbsp;@@my\_var  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def A.cv=(val)  
> &nbsp;&nbsp;&nbsp;&nbsp;@@my\_var = val  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;class \<\<self  
> &nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :my\_var  
> &nbsp;&nbsp;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 -  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class B \< A  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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
> 
> 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.

> **···**
>
> On 1/15/07, Nasir Khan \<rubylearner@gmail.com\> wrote:
> 
> > You had me at "I generally avoid class variables" 🙂
> > 
> > Here's another one you'll like:
> > 
> > &nbsp;&nbsp;&nbsp;@@avar = 1  
> > &nbsp;&nbsp;&nbsp;class A  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@avar = "hello"  
> > &nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;puts @@avar # =\> hello
> > 
> > &nbsp;&nbsp;&nbsp;A.class\_eval { puts @@avar } # =\> hello

---

<div class="post-metadata">

**Author:** ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)\
**Post date:** [16 January 2007 03:07 UTC](https://rubytalk.org/t/nuby-question-on-class-variable-and-class-instance-variable/34477/3 "2007-01-16T03:07:58Z")

</div>

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.

> **···**
>
> On 1/15/07, Nasir Khan \<rubylearner@gmail.com\> 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.
