instance variables and class variables in Ruby

Source: Internet
Author: User

The definition is simple: the instance variable @ begins, class variable @@ at the beginning to read some articles and books, the current understanding of (in fact, still do not understand, first remember, the wrong to change):
    1. Class variables: They can be accessed in class methods and class instance methods, can also be inherited by subclasses, and the values of the parent class are changed after the child class is modified.
      • Different subclasses share this value, including instances of subclasses, which can be modified. It is understood that, regardless of their own or sub-class, what functions can be accessed, can also be changed, after the change is also shared by everyone.
    2. Instance variables: can be accessed by class methods, cannot be inherited, and assignments in subclasses do not affect the parent class.
      • If the subclass is modified, does it actually redefine and assign a new value?
      • This is actually more like the public variables in the C++/c# class, and the subclass inherits the values that can be re-assigned, and the code is used as well.
      • But the instance variable cannot be accessed by the class instance method is a bit abstract ... It hurts to get a brain kernel with a similar noun.
I don't quite know what I'm talking about, I forget to read the code directly:
1 classTest2@ @class_var = 103@class_instance_var = 14   #class method: Both types of variables can be accessed5   defSelf.outclass_var6 puts @ @class_var7 End8   defSelf.outclass_instance_var9 puts @class_instance_varTen End One   #class instance method: Cannot access class instance variables A   defOutclass_var - puts @ @class_var - End the   defOutclass_instance_var - puts @class_instance_var - End - End +  - #If two variables are not initialized, they will not be printed here. Don't know if it's a version of the problem +Puts Test.class_variables#=>@ @class_var APuts Test.instance_variables#= @class_instance_var at  - #class variables that can be shared and modified by the quilt class - classSubcls <Test -@ @class_var = 12 - End -  in #you can see that the class variable values are now modified -Test.outclass_var#=>12 toSubcls.outclass_var#=>12 +  - #subclass cannot inherit instance variable of parent class theTest.outclass_instance_var#=>1 *Subcls.outclass_instance_var#= null $ Panax Notoginseng  - #class instance methods cannot access instance variables theTest.new.outclass_var#=>12 +Test.new.outclass_instance_var#Empty A  the #look at all of a sudden class, same as the parent class +SubCls.new.outclass_var#=>12 -SubCls.new.outclass_instance_var#Empty

instance variables and class variables in Ruby

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.