SoFunction
Updated on 2025-04-09

Introduction to Ruby Page 3/5


III. Variables and constants
Local variables
Identifiers starting with lowercase letters or underscores are local variables in Ruby (if an identifier that is not declared is referenced, it will be interpreted as a method call without arguments).
val = 5 if false
Although val has not been assigned, the declaration is still valid. At this time, the value of val is nil (equivalent to null in C#).

Instance variables
All instance variables are identifiers headed by the @ character, and the value of the uninitialized instance variable is nil.
class Employee @empId end
Class variables
Identifiers starting with @@ are class variables. The module variables defined in the module can be accessed by all classes containing the module.
1module Company 2 @@companyName = "Hello Ruby." 3 4 class Employee 5 def display 6 print "#@companyName" 7 end 8 end 9 10 class Department 11 def display 12 print "#@companyName" 13 end 14 end 15end
Global variables

Global variables do not need to be declared. All identifiers starting with $ are global variables that can be referenced anywhere in the program. The value of the unassigned global variable is nil.
1module Company 2 class Employee 3 def display 4 # nil 5 print "#$companyName" 6 $companyName = "Hello Ruby." 7 end 8 end 9 10 class Department 11 def display 12 # Hello Ruby. 13 print "#$companyName" 14 end 15 end 16end
 Pseudovariates

In Ruby there is an identifier called a pseudo-variable. The pseudo-variable is a bit like an environment variable, and it is also read-only.
# Execution body of the current method print "#{self}" # The only instance of the NilClass class print "#{nil}" # The only instance of TrueClass class print "#{true}" # The only instance of the FalseClass class print "#{false}" # Current source file name print "#{__FILE__}" # Line number in the current source file print "#{__LINE__}"
constant

An identifier headed in capital letters is a constant. A quadratic interpreter will prompt a warning when a constant is referenced to a constant that is not assigned, and a NameError exception is thrown. Constants defined outside a class or module belong to Object. You can use "::constant name" to refer to constants belonging to Object and refer to external constants in the form of "module name/class name::constant name".
1# Constant belonging to Object 2GroupName = "Stay at Home" 3 4module Site 5 SiteUrl = "http://" 6 7 class Sichuan 8 Add = "Liangshan" 9 # Refer to a constant belonging to an Object 10 print "#{::GroupName}" 11 end 12end 13 14# Directly refer to class name and module name 15# When referring to a module belonging to an Object, you can omit "::" 16print "#{::Site}\n#{Site::Sichuan}" 17# Constant belonging to the module 18print "#{Site::SiteUrl}" 19# Constants belonging to the class 20print "#{Site::Sichuan::Add}"
When referring to constants with the same name in modules and classes, referring to constants outside the nested tree first, Object has the lowest priority, but I suggest that you try not to use constants with the same name.
Previous page12345Next pageRead the full text