Ruby Variables
A variable is a storage location that holds any data that can be used by any program.
Ruby supports five types of variables.
- Generally start with lowercase letters or underscores: variable (Variable).
$$ at the beginning: global variable (Global variable).@@ at the beginning: instance variable (Instance variable).@@@@ at the beginning: class variable (Class variable). Class variables are shared throughout the inheritance chain.- Start with an uppercase letter: constant (Constant).
You have already learned about these variables in previous chapters. This chapter will explain these five types of variables in detail.
Ruby Global Variables
The value of an uninitialized global variable is nilnil, and using the -w option will produce a warning.
Assigning a value to a global variable changes the global state, so global variables are not recommended.
The following example shows the usage of global variables.
Example
Try it »
Here, $global_variable is a global variable. This will produce the following result:
Note:In Ruby, you can access the value of any variable or constant by placing the # character before the variable or constant.
全局变量在 Class1 中输出为 10 全局变量在 Class2 中输出为 10
Ruby Instance Variables
Instance variables start with @. The value of an uninitialized instance variable is nilnil, and using the -w option will produce a warning.
The following example shows the usage of instance variables.
Example
Try it »
Here, @cust_id, @cust_name, and @cust_addr are instance variables. This will produce the following result:
Customer id 1 Customer name John Customer address Wisdom Apartments, Ludhiya Customer id 2 Customer name Poul Customer address New Empire road, Khandala
Ruby Class Variables
Class variables start with @@, and must be initialized before they can be used in method definitions.
Referencing an uninitialized class variable produces an error. Class variables can be shared in subclasses or submodules of the class or module in which they are defined.
After using the -w option, redefining a class variable will produce a warning.
The following example shows the usage of class variables.
Example
Try it »
Here, @@no_of_customers is a class variable. This will produce the following result:
Total number of customers: 1 Total number of customers: 2
Ruby Local Variables
Local variables start with a lowercase letter or an underscore _. The scope of a local variable is from class, module, def, or do to the corresponding end, or from the left brace to the right brace {}.
When an uninitialized local variable is called, it is interpreted as calling a method without arguments.
Assigning a value to an uninitialized local variable can also be regarded as a variable declaration. The variable persists until the end of the current scope. The lifetime of a local variable is determined when Ruby parses the program.
In the above example, the local variables are id, name, and addr.
Ruby Constants
Constants start with an uppercase letter. Constants defined inside a class or module can be accessed from within the class or module, while constants defined outside a class or module can be accessed globally.
Constants cannot be defined inside methods. Referencing an uninitialized constant produces an error. Assigning a value to an already initialized constant produces a warning.
Example
Try it »
Here, VAR1 and VAR2 are constants. This will produce the following result:
第一个常量的值为 100 第二个常量的值为 200
Ruby Pseudo-Variables
They are special variables that look like local variables but behave like constants. You cannot assign any value to these variables.
- self:The receiver object of the current method.
- true:Represents the value true.
- false:Represents the value false.
- nil:Represents the value undefined.
- __FILE__:The name of the current source file.
- __LINE__:The line number of the current line in the source file.