Ruby Variables
A variable is a storage location that holds any data that can be used by a program.
Ruby supports five types of variables.
- Starting with a lowercase letter or underscore: variable (Variable).
$Starting with: global variable (Global variable).@Starting with: instance variable (Instance variable).@@Starting with: class variable (Class variable); class variables are shared throughout the entire inheritance chain- Starting with an uppercase letter: constant (Constant).
You have already learned about these variables in general in previous chapters. This chapter explains these five types of variables in detail.
Ruby Global Variables
Global variables start with $. The value of an uninitialized global variable is nil, and a warning is produced when the -w option is used.
Assigning to a global variable changes the global state, so the use of global variables is not recommended.
The following example shows the use of global variables.
Example
!/usr/bin/ruby
-- coding: UTF-8 --
$global_variable
=
10
class
Class1
def
print_global
puts
"
The global variable is output in Class1 as #$global_variable
"
end
end
class
Class2
def
print_global
puts
"
The global variable is output in Class2 as #$global_variable
"
end
end
class1obj
=
Class1
.
new
class1obj
.
print_global
class2obj
=
Class2
.
new
class2obj
.
print_global
Try it »
Here, $global_variable is a global variable. This produces the following result:
**Note:** In Ruby, you can access the value of any variable or constant by placing a # 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 nil, and a warning is produced when the -w option is used.
The following example shows the use of instance variables.
Example
!/usr/bin/ruby
class
Customer
def
initialize
(
id
,
name
,
addr
)
@cust_id
=
id
@cust_name
=
name
@cust_addr
=
addr
end
def
display_details
(
)
puts
"
Customer id #@cust_id
"
puts
"
Customer name #@cust_name
"
puts
"
Customer address #@cust_addr
"
end
end
Creating objects
cust1
=
Customer
.
new
(
"
1
"
,
"
John
"
,
"
Wisdom Apartments, Ludhiya
"
)
cust2
=
Customer
.
new
(
"
2
"
,
"
Poul
"
,
"
New Empire road, Khandala
"
)
Call the method
cust1
.
display_details
(
)
cust2
.
display_details
(
)
Try it »
Here, @cust_id, @cust_name, and @cust_addr are instance variables. This produces 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 a method definition.
Referencing an uninitialized class variable produces an error. Class variables can be shared among subclasses or submodules of the class or module that defines them.
Overriding a class variable produces a warning when the -w option is used.
The following example shows the use of class variables.
Example
!/usr/bin/ruby
class
Customer
@@no_of_customers
=
0
def
initialize
(
id
,
name
,
addr
)
@cust_id
=
id
@cust_name
=
name
@cust_addr
=
addr
end
def
display_details
(
)
puts
"
Customer id #@cust_id
"
puts
"
Customer name #@cust_name
"
puts
"
Customer address #@cust_addr
"
end
def
total_no_of_customers
(
)
@@no_of_customers
+=
1
puts
"
Total number of customers: #@@no_of_customers
"
end
end
Creating objects
cust1
=
Customer
.
new
(
"
1
"
,
"
John
"
,
"
Wisdom Apartments, Ludhiya
"
)
cust2
=
Customer
.
new
(
"
2
"
,
"
Poul
"
,
"
New Empire road, Khandala
"
)
Call the method
cust1
.
total_no_of_customers
(
)
cust2
.
total_no_of_customers
(
)
Try it »
Here, @@no_of_customers is a class variable. This produces the following result:
Total number of customers: 1
Total number of customers: 2
Ruby Local Variables
Local variables start with a lowercase letter or the underscore _. The scope of a local variable runs from class, module, def, or do to the corresponding end, or from the left curly brace to the right curly brace {}.
When an uninitialized local variable is referenced, it is interpreted as a call to a method with no arguments.
Assigning to an uninitialized local variable can also be regarded as a variable declaration. The variable exists until the end of the current scope. The lifetime of a local variable is determined when Ruby parses the program.
In the example above, 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 that class or module, while constants defined outside a class or module can be accessed globally.
Constants cannot be defined inside a method. Referencing an uninitialized constant produces an error. Assigning to an already initialized constant produces a warning.
Example
!/usr/bin/ruby
-- coding: UTF-8 --
class
Example
VAR1
=
100
VAR2
=
200
def
show
puts
"
The value of the first constant is #{VAR1}
"
puts
"
The value of the second constant is #{VAR2}
"
end
end
Creating objects
object
=
Example
.
new
(
)
object
.
show
Try it »
Here, VAR1 and VAR2 are constants. This produces the following result:
第一个常量的值为 100
第二个常量的值为 200
Ruby Pseudo-Variables
These are special variables that have the appearance of local variables but behave like constants. You cannot assign any value to these variables.
- self: The receiver object of the current method.
- true: The value representing true.
- false: The value representing false.
- nil: The value representing undefined.
- FILE: The name of the current source file.
- LINE: The line number of the current line in the source file.