English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Variables are storage locations that hold any data that can be used by any program.
Ruby supports five types of variables.
Generally lowercase letters or underscores at the beginning: Variables (Variable).
Starting with $: Global variables (Global variable).
Starting with @: Instance variables (Instance variable).
Starting with @@: Class variables (Class variable) Class variables are shared throughout the inheritance chain
Uppercase letters at the beginning: Constants (Constant).
You have gained a general understanding of these variables in the previous chapters. This chapter will explain the five types of variables in detail.
Global variables start with $. The value of an uninitialized global variable is nilwhen using -A warning will be generated after the w option.
Assigning values to global variables will change the global state, so it is not recommended to use global variables.
The following example shows the usage of global variables.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $global_variable = 10 class Class1 def print_global puts "Global variables in Class1 is output as #$global_variable" end end class Class2 def print_global puts "Global variables in Class2 is output as #$global_variable" end end class1obj = Class1.new class1obj.print_global class2obj = Class2.new class2obj.print_global
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.
Global variables in Class1 and output as 10 Global variables in Class2 and output as 10
Example variables start with @. The value of an uninitialized example variable is nilwhen using -A warning will be generated after the w option.
The following example shows the usage of example variables.
#!/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 # Create object cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala") # Call method cust1.display_details() cust2.display_details()
Here, @cust_id, @cust_name, and @cust_addr are example 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
Class variables must start with @@ and must be initialized before they can be used in method definitions.
Referencing an uninitialized class variable will cause an error. Class variables can be shared and used in subclasses or submodules of the class or module in which they are defined.
In use -After the w option, redefining class variables will produce a warning.
The following example demonstrates the usage of class variables.
#!/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 # Create object cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala") # Call method cust1.total_no_of_customers() cust2.total_no_of_customers()
In this case, @@no_of_customers is a class variable. This will result in the following:
Total number of customers: 1 Total number of customers: 2
Local variables start with lowercase letters or an underscore _. The scope of local variables extends from class, module, def, or do to the corresponding end or from the left curly brace { to the right curly brace }.
When calling an uninitialized local variable, it is interpreted as calling a method without any parameters.
Assigning a value to an uninitialized local variable can also be considered as variable declaration. The variable will exist until the end of the current scope. The lifecycle of local variables in Ruby is determined during the parsing of the program.
In the above example, the local variables are id, name, and addr.
Constants are written with uppercase letters. Constants defined within a class or module can be accessed from within the class or module, while constants defined outside of a class or module can be accessed globally.
Constants cannot be defined within methods. Referencing an uninitialized constant will raise an error. Assigning a value to an already initialized constant will produce a warning.
#!/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 # Create object object=Example.new() object.show
Here, VAR1 and VAR2 is a constant. This will produce the following result:
The value of the first constant is 100 The value of the second constant is 200
They are special variables that have the appearance of local variables but behave like constants. You cannot assign any values to these variables.
self: The receiver object of the current method.
true: Represents the value of true.
false: Represents the value of false.
nil: Represents the value of undefined.
__FILE__: The name of the current source file.
__LINE__: The line number in the source file.