Global variables, instance variables, class variables, and symbol in ruby

Source: Internet
Author: User

Reference: http://rubyer.me/blog/485

Http://sunnyshuhai.iteye.com/blog/286970

In Ruby, global variables, instance variables, class variables, and symbol are often very different from other languages such as Java and are easy to mix up.


First, there are no naming conventions. Note that these conventions are mandatory:

Format Name Whether Initialization is required Scope Example
Starting with $ Global Variables No. Nil is used before the initialization. Starting from the definition, until the end of the program $ User
@ Instance variables No. Nil is used before the initialization. Self @ User
@ Class variable You must first define and directly use the exception Direct internal use, external use :: @ User
Starting with [a-Z] Or _ Local variable Nil is not assigned before initialization. You must assign a value before calling it. Pseudo variables: Self, nil, _ file _, etc. Defined classes, modules, and methods cannot be shared among classes, modules, and methods. User
A-Z Constant Values can only be assigned once. If values are assigned multiple times, a warning is displayed, but no error is returned. Internal and external. External access "Class Name: constant name" User
: Symbol No Internal and external : User
Note the following when using local variables:

1. You can define the top-level local variable, but the variable cannot be accessed within the class, method, and module definition;

2. Local variables cannot be accessed in the internal class or subclass; Methods in the class cannot access the local variables defined in the class (level );

3. Within the class, module, and method for declaring a local variable, the local variable takes effect from the declared variable and ends with the definition of the class, method, and module;

Note the following when using global variables:

1. Try to use less, because it will cause strong coupling between modules.

Note the following when using instance variables:

1. instance variables are usually defined in methods;

When an instance variable is declared in the method, the instance variable actually belongs to the class of the method, rather than the method;

Although the instance variables belong to the class, most of the time we define the instance variables in the method of this class;

2. When creating an object, Java allocates memory space for all instance variables of the object at one time;

Objects in ruby are completely dynamic. when an object is created, it does not have any instance variables until it is defined as an instance variable;

3. The access scope of instance variables is always private, that is, there is no restriction on the Value assignment and reading of instance variables within the class definition. When you access instance variables outside the class, you can access the instance variables through the method.

4. instance variables cannot be exposed to public. They can only be exposed through class methods (class name. method name ).

5. instance variables defined in the module can be mixed into the classes in the module. Classes in the module can share the instance variables defined in the module.

Notes for class variables:

1. A class, its subclass, and its instances: class variables with the same name share the same memory region (all referenced the same variable );

2. The class variables defined in the module can be mixed into the classes in the module. Classes in the module can share class variables defined in the module.

Variables in the ruby class are private, and the method is public by default.

Note:

1. symbol is the name of instance variables, methods, classes, etc.

2. If a method is move_left, a symbol is automatically generated: move_left.

3. Ruby maintains a symbol table internally to save the symbol. You can call symbol. all_symbols to check what is there.

4. Each string object is different. Even if they contain the same string content, a name (string content) uniquely identifies a symbol object.

5. Do not declare symbol. The system will automatically be unique. It is just a name and an internal ID. Symbols is very useful because in Ruby programs, symbol always points to the same object.

Differences between class variables and constants:

1. class variables can be assigned values repeatedly. A warning (not an error) is issued when constants are assigned values repeatedly );

2. class variables are protected by default and cannot be directly referenced outside the class; (values can be used or assigned to the inherited class );

Differences between class variables and instance variables:

1. class variables defined within the class scope can be accessed in the class methods, but instance variables cannot;

2. class variables can be referenced or assigned values in child classes, but instance variables can be directly referenced or assigned values within the class scope;

When do I need to use symbol?

1. If the object content (Character Sequence) is very important, use string

2. If the object's consistent identity is very important, use symbol.

3. A large number of repeated content, such as the key in hash, usually use symbol.

The symbol is an integer that increases the hash retrieval speed.

References:

Http://rubylearning.com/satishtalim/ruby_symbols.html

Http://www.cnblogs.com/fanyangxi/articles/1632410.html

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.