Can anybody tell me about the differences between class variables and square instance variables?
between a square variable ( @@ ) and all its descendants The class instance variables are shared ( @ ) by the descendants of the class.
Class variables ( @@ )
class Foo @@ i = 1 def self.i @@ i end def self.i = (value) @@ i = value end end and a derivative category:
class bar & lt; Foo End
We see that Foo and Bar have the same value for @@ i :
p Foo .i # = & gt; 1 p bar.i # = & gt; 1 and changing it to @@ i it is converted to both:
bar.i = 2p Foo # = & gt; 2p bar.i # = & gt; 2 Class example variable ( @ )
Let's have a simple class with a square To create an example, type the variable @i and @i to read and write:
class Foo @i = 1 def self .i @i end def self.i = (value) @i = value expiration and a derived class:
class bar & lt; Foo End We see that though the accessory code for the bar was not found by @ i , it is not @i :
p foo.i # = & gt; 1 p bar.i # = & gt; Zero We can set the bar without affecting the @i of @i :
bar = I = 2 pu FU # => 1 p bar.i # = & gt; 2
Comments
Post a Comment