Ruby variables include local variables, global variables, instance variables, class variables, and constants.
1. Local Variables
Local variables start with a lowercase letter or underline
Local variables have local scope restrictions (such as within a block). The scope of a local variable starts from the Declaration and ends with the block, method definition, and class/module definition of the Declaration. This is often the case when you write code:
IRB (main): 001: 0> I = 123 ---------- here I is a local variable
=> 123
IRB (main): 002: 0> S = "hi" ---------- here s is the local variable
=> "Hi"
Ruby variables are dynamic variables. A variable is a numeric variable at the previous moment, and can be a numeric variable at the next moment:
IRB (main): 003: 0> X = 321
=> 321
IRB (main): 004: 0> X = "hello"
=> "Hello"
Ruby is a dynamic variable, but it is a strong type. For example, the characters and numbers cannot be directly added:
IRB (main): 005: 0> X = 10
=> 10
IRB (main): 006: 0> Y = "hi"
=> "Hi"
IRB (main): 007: 0> X + Y
Typeerror: string can't be coerced into fixnum
From (IRB): 7: In '+'
From (IRB): 7
From: 0
Manual conversion is required: IRB (main): 008: 0> X. to_s + Y
=> "10hi"
2. Global Variables
Ruby's global variables start with $For example, $ x $ Y. Global variables can be referenced anywhere in the program. No variable declaration is required for global variables. When Referencing uninitialized global variables, the value is nil
Ruby has built-in global variables, which should be copied from Perl, for example, $! Record the most recent error, $. indicates the row number. Good programming practice is that global variables are not used and they are dangerous and difficult to trace.
3. instance variables
Ruby instance variables start @Is the variable bound to the instantiated object. Instance variables are specific objects. For example:
IRB (main): 016: 0> class myclass
IRB (main): 017: 0> def initialize (name, gender, age)
IRB (main): 018: 0> @ name = Name
IRB (main): 019: 0> @ gender = gender
IRB (main): 020: 2> @ age = Age
IRB (main): 021: 0> end
IRB (main): 022: 0> end
=> Nil --------------- @ name, @ gender, and @ age are all instance variables. Instance variables can be referenced in class or subclass methods. If you reference an instance variable that has not been initialized, its value is nil.
IRB (main): 023: 0> X = myclass. New ("John ")
### <Myclass: 0x7f2e15a7dad8 @ name = "John">
Myclass class. Its constructor receives a name parameter and assigns this parameter to the instance variable @ name.
X is an instance of myclass, And she owns the instance variable @ name.
Instance variables are generated and exist only when classes are instantiated. However, instance objects cannot directly access instance variables:
IRB (main): 022: 0> [email protected]
Syntaxerror: Compile Error
(IRB): 22: syntax error, unexpected tivar
From (IRB): 22
From: 0
This is wrong. The get method must be defined in the; class to access instance variables:
IRB (main): 023: 0> class myclass
IRB (main): 024: 1> def name
IRB (main): 025: 2> @ name
IRB (main): 026: 2> end
IRB (main): 027: 1> end
=> Nil
IRB (main): 028: 0> X. Name
=> "John"
Of course, you can also define the set method to set instance variables:
IRB (main): 029: 0> class myclass
IRB (main): 030: 1> def name = (value)
IRB (main): 031: 2> @ name = Value
IRB (main): 032: 2> end
IRB (main): 033: 1> end
=> Nil
IRB (main): 034: 0> X. Name = "Jean"
=> "Jean"
IRB (main): 035: 0> X. Name
=> "Jean"
This set and get can be implemented through Ruby metaprogramming, for example:
IRB (main): 036: 0> class myclass
IRB (main): 037: 1> attr_accessor: Age
IRB (main): 038: 1> end
=> Nil
IRB (main): 039: 0> X. Age = 20
=> 20
IRB (main): 040: 0> X. Age
=> 20
You only need to set attr_accessor. It will create the set and get methods for the instance variable @ age.
IRB (main): 041: 0> X
==#< Myclass: 0x7f2e15a7dad8 @ name = "Jean", @ age = 20>
The corresponding attr_reader only sets the get method, and attr_writer only sets the set method.
4. class variables
Ruby class variables start @For example, @ x @ Y stated in the class is rarely used.
Class variables are defined in the definition of classes. class variables can be assigned and referenced in special methods and instance methods of classes. Class variables are shared by classes, subclass of classes, and their instance objects.
Class person
@ Number = 0 # The initial value must exist before use
Def initialize (name, gender, age)
@ Age = Age
@ Number + = 1
End
End
Class variables are private and cannot be directly accessed outside the class. You can only access them through instance methods and class methods. Class variables can be viewed as global variables shared by classes, subclasses, and their instances.
The class variables defined in the module are shared by all classes that contain the module.
Module testmodule
@ Foo = 10
End
Class Klass
Include foo
P @ Foo + = 1 # => 11
End
Class base
Include foo
P @ Foo + = 2 # => 12
End
5. Constants
Ruby constants start with an uppercase letter. The definition and initialization of constants are completed by the assignment process.
IRB (main): 048: 0> Pi = 3.14 ---------- pi is a constant.
=> 3.14
However, Ruby constants can be changed. If you assign values to a defined constant, a warning message is displayed:
IRB (main): 049: 0> Pi = 3.15
(IRB): 49: Warning: already initialized constant pi
=> 3.15
IRB (main): 050: 0> pi
=> 3.15
Despite the warning, the constant is indeed changed.
Note: 1) if an undefined constant is referenced, A nameerror exception is thrown.
2) constants can be defined in classes and modules, and cannot be defined in methods.
Usually set constants in the class:
IRB (main): 051: 0> class myclass
IRB (main): 052: 1> Pi = 3.1415
IRB (main): 053: 1> end
=> 3.1415
3) use the ":" operator if you want to be a constant in an external struct class or module.
Access this constant from outside the class: IRB (main): 055: 0> myclass: pi
=> 3.1415
The same applies to modules. For example, the PI constant used to access the built-in math module of Ruby: IRB (main): 056: 0> Math: pi
=> 3.14159265358979
4) When a class definition expression is used to generate a class object, the class object is assigned a constant with the same name as the class object. The referenced class name refers to the constant.
Class Test
End
P test. class # class
P test # Test
To access constants in the object class (constants at the top layer), you also need to use the ":" operator, but the left side of the operator is null.
Differences and relationships between instance variables and class variables in ruby