Variables in Ruby (record here to avoid forgetting ^-^)

Source: Internet
Author: User

If a variable can only be used by an instance object, such variables are called instance variables. If a variable can be shared by all instance objects of a class, such variables are called class variables.

 

1) constants can be defined in classes and modules, but not in methods. If a constant in an external struct class or module, use the domain operator ::

 

2) The global variable starts with $.

3) instance variable. The variable name starts with @; the variable name of the class variable starts @

 

4) The local variables mentioned in Ruby can exist in classes, methods, modules, loops, and process objects. The local variable name starts with a lowercase letter.

 

5) a class of things can share a variable and become a class variable. Unlike global variables and instance variables, class variables must be initialized before use. If global variables and instance variables are not initialized, the value is nil.

 

6) call a class method. Like defining a class method, you must add a class name and a period (.) before the method name. Class methods provide a way to implement class variables outside the class without passing the class instance methods.
Class variables, which correspond to static variables and static methods in Java.

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.