Ruby variable
A variable is a storage location that holds any data that can be used by anyprogram.
Ruby supports five types of variables.
General lowercase letters, beginning with an underscore: variable.
$
Beginning: Global variable.@
Beginning: Instance variable.@@
Beginning: Class variable are shared in the entire inheritance chainStart with a capital letter: Constant.
You have already learned about these variables in the previous section, and this section will explain these five types of variables in detail.
Ruby global variable
Global variables to $
the beginning. The value of an uninitialized global variable is nil
, in use of -w
option, a warning is generated.
Assigning values to global variables changes the global state, so it is not recommended to use global variables.
The following example shows the use of global variables.
Example
#!/usr/bin/ruby#-*- coding: UTF-8
-*-$global_variable=10classClass1defprint_globalputs"Global variables output in Class1 as
#$global_variable"endendclassClass2defprint_globalputs"Global variables output in Class2 as
#$global_variable"endendclass1obj=Class1.newclass1obj.print_globalclass2obj=Class2.newclass2obj.print_global
Here, $global_variable
is a global variable. This will produce the following results:
Note: in Ruby, you can put it in front of a variable or constant #
character to access the value of any variable or constant.
Global variables output in Class1 as 10
Global variables output in Class2 as 10
Ruby instance variable
Instance variable to @
the beginning. The value of an uninitialized instance variable is nil
, in use of -w
option, a warning is generated.
The following example shows the use of instance variables.
Example
#!/usr/bin/rubyclassCustomerdefinitialize(id,name,addr)@cust_id=id@cust_name=name@cust_addr=addrenddefdisplay_details()puts"Customer
id #@cust_id"puts"Customer name #@cust_name"puts"Customer address
#@cust_addr"endend#create objectcust1=Customer.new("1","John","Wisdom
Apartments, Ludhiya")cust2=Customer.new("2","Poul","New Empire road,
Khandala")#calling methodcust1.display_details()cust2.display_details()
Here, @cust_id
、 @cust_name
and @cust_addr
are instance variable. This will produce the following results:
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 variable
Class variables to @@
at the beginning and must be initialized before it can be used in the method definition.
Referencing an uninitialized class variable results in an error. A class variable can be shared among subclasses or submodules of the class or modulein which it is defined.
In use -w
option, overloading the class variable generates a warning.
The following example shows the use of class variables.
Example
#!/usr/bin/rubyclassCustomer@@no_of_customers=0definitialize(id,name,addr)@cust_id=id@cust_name=name@cust_addr=addrenddefdisplay_details()puts"Customer
id #@cust_id"puts"Customer name #@cust_name"puts"Customer address
#@cust_addr"enddeftotal_no_of_customers()@@no_of_customers+=1puts"Total
number of customers:
#@@no_of_customers"endend#create objectcust1=Customer.new("1","John","Wisdom
Apartments, Ludhiya")cust2=Customer.new("2","Poul","New Empire road,
Khandala")#calling methodcust1.total_no_of_customers()cust2.total_no_of_customers()
Here, @@no_of_customers
is a class variable. This will produce the following results:
Total number of customers: 1
Total number of customers: 2
Ruby local variable
Local variables are lowercase or underlined \_
the beginning. The scope of local variables ranges 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 callinga method without parameters.
Assigning values to uninitialized local variables can also be treated as variable declarations. The variable will exist until the end of the current domain. The life cycle of a local variable is determined when the Ruby parser is used.
In the above example, the local variable is id
、 name
and addr
.
Ruby constant
Constants begin with uppercase letters. Constants defined within a class or module can be accessed from within the class or module, and constants defined outside the class or module can be accessed globally.
Constants cannot be defined in a method. Referencing an uninitialized constant results in an error. A warning is generated for a constant assignment that has been initialized.
Example
#!/usr/bin/ruby#-*- coding: UTF-8
-*-classExampleVAR1=100VAR2=200defshowputs"The value of the first constant is
#{VAR1}"puts"The value of the second constant is
#{VAR2}"endend#create objectobject=Example.new()object.show
Here, VAR1
and VAR2
are constants. This will produce the followingresults:
The value of the first constant is 100
The value of the second constant is 200
Ruby pseudo variable
They are special variables that look like local variables, but behave like constants. You cannot assign any values to these variables.
self
:The sink object for the current methodtrue
:Representativetrue
value.false
:Representativefalse
value.nil
:Representativeundefined
value.\__FILE__
:The name of the current source file.\__LINE__
:The number of the current line in the source file.