# Class variables and class instance variables in Pickaxe 2nd

**URL:** <https://rubytalk.org/t/class-variables-and-class-instance-variables-in-pickaxe-2nd/27604>\
**Category:** ruby-talk\
**Created:** [21 May 2006 03:52 UTC](https://rubytalk.org/t/class-variables-and-class-instance-variables-in-pickaxe-2nd/27604 "2006-05-21T03:52:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Uncutstone\_Wu](https://avatars.discourse-cdn.com/v4/letter/u/82dd89/32.png) [@Uncutstone\_Wu](https://rubytalk.org/u/Uncutstone_Wu)\
**Post date:** [21 May 2006 03:52 UTC](https://rubytalk.org/t/class-variables-and-class-instance-variables-in-pickaxe-2nd/27604/1 "2006-05-21T03:52:45Z")

</div>

These are two concepts in Pickaxe 2nd edtion.

Class Variables example is in chapter 3:

class Song  
&nbsp;&nbsp;@@plays = 0  
&nbsp;&nbsp;def play  
&nbsp;&nbsp;&nbsp;&nbsp;@@plays += 1  
&nbsp;&nbsp;end  
end

Class instance variables example is in chapter 24:

class Test  
&nbsp;&nbsp;@cls\_var = 123  
&nbsp;&nbsp;def Test.inc  
&nbsp;&nbsp;&nbsp;&nbsp;@cls\_var += 1  
&nbsp;&nbsp;end  
end

The only difference I can see is the names of the variables, one begins  
with @, and the other begins with @@.  
Are they same? I feel confused if they are same , why give two names?

Thanks in advance.

uncutstone

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [21 May 2006 04:04 UTC](https://rubytalk.org/t/class-variables-and-class-instance-variables-in-pickaxe-2nd/27604/2 "2006-05-21T04:04:01Z")

</div>

Class variables are accessible to instances of the class directly. Class variables also propagate thru the inheritance hierarchy. Class instance variables are just like any other instance variables, they just happen to be associated with an object of class Class.

> **···**
>
> On May 20, 2006, at 11:52 PM, uncutstone wu wrote:
> 
> > These are two concepts in Pickaxe 2nd edtion.
> > 
> > Class Variables example is in chapter 3:
> > 
> > class Song  
> > &nbsp;&nbsp;@@plays = 0  
> > &nbsp;&nbsp;def play  
> > &nbsp;&nbsp;&nbsp;&nbsp;@@plays += 1  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Class instance variables example is in chapter 24:
> > 
> > class Test  
> > &nbsp;&nbsp;@cls\_var = 123  
> > &nbsp;&nbsp;def Test.inc  
> > &nbsp;&nbsp;&nbsp;&nbsp;@cls\_var += 1  
> > &nbsp;&nbsp;end  
> > end
> > 
> > The only difference I can see is the names of the variables, one begins  
> > with @, and the other begins with @@.  
> > Are they same? I feel confused if they are same , why give two names?
> > 
> > Thanks in advance.
> > 
> > uncutstone
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [21 May 2006 04:34 UTC](https://rubytalk.org/t/class-variables-and-class-instance-variables-in-pickaxe-2nd/27604/3 "2006-05-21T04:34:38Z")

</div>

uncutstone wu wrote:

> These are two concepts in Pickaxe 2nd edtion.
> 
> Class Variables example is in chapter 3:
> 
> class Song  
> &nbsp;&nbsp;@@plays = 0  
> &nbsp;&nbsp;def play  
> &nbsp;&nbsp;&nbsp;&nbsp;@@plays += 1  
> &nbsp;&nbsp;end  
> end

Note that the class variable is being used here by an instance method.

> Class instance variables example is in chapter 24:
> 
> class Test  
> &nbsp;&nbsp;@cls\_var = 123  
> &nbsp;&nbsp;def Test.inc  
> &nbsp;&nbsp;&nbsp;&nbsp;@cls\_var += 1  
> &nbsp;&nbsp;end  
> end

And note that the class instance variable is being used here by a class method.

A class is also an object; it's an instance of Class. Since it is an object it may have its own instance variables. Class variables are available to the class and to instances of that class; class instance variables are only visible to the class itself (not to instances of that class). (Yeah, it can be confusing at first.)

class Foo  
&nbsp;&nbsp;&nbsp;@@cls\_var = 123  
&nbsp;&nbsp;&nbsp;@cls\_instvar = 'abc'

&nbsp;&nbsp;&nbsp;def test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p @@cls\_var  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p @cls\_instvar

&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def self.test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p @@cls\_var  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p @cls\_instvar  
&nbsp;&nbsp;&nbsp;end  
end

puts( "Foo is a #{Foo.class}")

f = Foo.new  
f.test  
Foo.test

> **···**
>
> --  
> James Britt
> 
> [http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
> [Ruby Code & Style](http://www.artima.com/rubycs/) - The Journal By & For Rubyists  
> [http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
> [http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
> [http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools

---

<div class="post-metadata">

**Author:** ![Uncutstone\_Wu](https://avatars.discourse-cdn.com/v4/letter/u/82dd89/32.png) [@Uncutstone\_Wu](https://rubytalk.org/u/Uncutstone_Wu)\
**Post date:** [21 May 2006 04:40 UTC](https://rubytalk.org/t/class-variables-and-class-instance-variables-in-pickaxe-2nd/27604/4 "2006-05-21T04:40:34Z")

</div>

Logan Capaldo wrote:

> **···**
>
> > On May 20, 2006, at 11:52 PM, uncutstone wu wrote:
> > 
> > > begins
> > 
> > Class variables are accessible to instances of the class directly.  
> > Class variables also propagate thru the inheritance hierarchy. Class  
> > instance variables are just like any other instance variables, they  
> > just happen to be associated with an object of class Class.
> 
> Very thanks, you make it clear to me now. Can I say class instance  
> variables are totally private nobody except the class own it can access  
> it, and by default, class variables are public to object instances and  
> subclasses?
> 
> uncutstone
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Uncutstone\_Wu](https://avatars.discourse-cdn.com/v4/letter/u/82dd89/32.png) [@Uncutstone\_Wu](https://rubytalk.org/u/Uncutstone_Wu)\
**Post date:** [21 May 2006 04:47 UTC](https://rubytalk.org/t/class-variables-and-class-instance-variables-in-pickaxe-2nd/27604/5 "2006-05-21T04:47:26Z")

</div>

James Britt wrote:

> A class is also an object; it's an instance of Class. Since it is an  
> object it may have its own instance variables. Class variables are  
> available to the class and to instances of that class; class instance  
> variables are only visible to the class itself (not to instances of that  
> class). (Yeah, it can be confusing at first.)
> 
> class Foo  
> &nbsp;&nbsp;&nbsp;@@cls\_var = 123  
> &nbsp;&nbsp;&nbsp;@cls\_instvar = 'abc'
> 
> &nbsp;&nbsp;&nbsp;def test  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p @@cls\_var  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p @cls\_instvar
> 
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;def self.test  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p @@cls\_var  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p @cls\_instvar  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> puts( "Foo is a #{Foo.class}")
> 
> f = Foo.new  
> f.test  
> Foo.test

Very thanks, it's quite clear now.

uncutstone

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
