Forum: Ruby Class variables and class instance variables in Pickaxe 2nd

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
288733e11db1c6dbbf50871d2c6738c6?d=identicon&s=25 Uncutstone Wu (uncutstone)
on 2006-05-21 05:52
These are two concepts in Pickaxe 2nd edtion.

Class Variables example is in chapter 3:

class Song
  @@plays = 0
  def play
    @@plays += 1
  end
end

Class instance variables example is in chapter 24:

class Test
  @cls_var = 123
  def Test.inc
    @cls_var += 1
  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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-21 06:05
(Received via mailing list)
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.
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-05-21 06:36
(Received via mailing list)
uncutstone wu wrote:
> These are two concepts in Pickaxe 2nd edtion.
>
> Class Variables example is in chapter 3:
>
> class Song
>   @@plays = 0
>   def play
>     @@plays += 1
>   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
>   @cls_var = 123
>   def Test.inc
>     @cls_var += 1
>   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
   @@cls_var = 123
   @cls_instvar = 'abc'

   def test
     p @@cls_var
     p @cls_instvar

   end

   def self.test
     p @@cls_var
     p @cls_instvar
   end
end


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

f = Foo.new
f.test
Foo.test




--
James Britt

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - The Journal By & For Rubyists
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools
288733e11db1c6dbbf50871d2c6738c6?d=identicon&s=25 Uncutstone Wu (uncutstone)
on 2006-05-21 06:40
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
288733e11db1c6dbbf50871d2c6738c6?d=identicon&s=25 Uncutstone Wu (uncutstone)
on 2006-05-21 06:47
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
>    @@cls_var = 123
>    @cls_instvar = 'abc'
>
>    def test
>      p @@cls_var
>      p @cls_instvar
>
>    end
>
>    def self.test
>      p @@cls_var
>      p @cls_instvar
>    end
> end
>
>
> puts( "Foo is a #{Foo.class}")
>
> f = Foo.new
> f.test
> Foo.test

Very thanks, it's quite clear now.

uncutstone
This topic is locked and can not be replied to.