Home »
Ruby Tutorial
Variables in Ruby
By IncludeHelp Last updated : November 16, 2024
Ruby supports the following types of variables:
- Instance variable
- Class variable
- Local variable
- Global variable
1. Instance variable
An instance variable always starts with @. If you don't initialize an instance variable then it will get a nil value from compiler at the compile time. Their values are limited or local to certain instances of an object. There exist multiple copies of an instance variable and every object of the same class may have access to their local copy of instance variable. Any changes they made on the instance variable will not be reflected in the other's copy of that variable. Every copy of instance variable is independent of each other.
Syntax
@variable_name
Example
class Student
def initialize(id, name, addr)
@stu_id = id
@stu_name = name
@stu_addr = addr
end
def display_details()
puts "Student id #@stu_id"
puts "Student name #@stu_name"
puts "Student address #@stu_addr"
end
end
stu1 = Student.new("1", "Karishma", "Raiwala")
stu2 = Student.new("2", "Kayra", "Bhopal")
stu1.display_details()
stu2.display_details()
Output
Student id 1
Student name Karishma
Student address Raiwala
Student id 2
Student name Kayra
Student address Bhopal
2. Class variable
A class variable is always declared with @@. If you don't initialize a class variable then you face an error at the compile time. Alike, Instance Variable, it is not implicitly initialized with nil. There is only one copy of a Class Variable and it is shared by the different objects of the same class. Any changes made by any object in the class variable will be reflected in every object that is accessing it. The class variable belongs to the whole class and is accessible anywhere inside the class.
Syntax
@@variable_name
Example
class Student
@@no_of_students = 0
def initialize(id, name, addr)
@stu_id = id
@stu_name = name
@stu_addr = addr
end
def display_details()
puts "Student id #@stu_id"
puts "Student name #@stu_name"
puts "Student address #@stu_addr"
end
def total_no_of_students()
@@no_of_students +=1
puts "Total number of students are #@@no_of_students"
end
end
stu1 = Student.new("1", "Karishma", "Raiwala")
stu2 = Student.new("2", "Kayra", "Bhopal")
stu1.display_details()
stu1.total_no_of_students()
stu2.display_details()
stu1.total_no_of_students()
Output
Student id 1
Student name Karishma
Student address Raiwala
Total number of students are 1
Student id 2
Student name Kayra
Student address Bhopal
Total number of students are 2
3. Local variable
Local variables always start with '_' or lowercase letters (a-z). You will get an error by the compiler if you want to access the local variable outside the method in which it is declared. The scope of a local variable starts at def, do, or opening braces { and ends with the keyword end or with the closing braces }. There is no need to initialize a local variable. The lifetime of the local variable is decided or determined when the compiler compiles the code.
Syntax
_(variable_name) or (variable_name) in lowercase
Example
class Student
def update
puts "Enter the number of students"
no_of_students=gets.chomp
puts "The updated numbers of students are #{no_of_students}"
end
end
record1=Student.new
record1.update
Output
Enter the number of students
36
The updated numbers of students are 36
4. Global variable
A global variable always starts with $. nil is provided to an uninitialized global variable and creates the warnings with –w option. The scope of a class variable is limited to the methods which are defined inside the class but if you want to declare a variable which is not limited only to a single class but can be accessed by multiple classes, in such case Global variable comes into the scenario. By default, an uninitialized global variable has a nil value. Global variables are least preferred or most of the times they are avoided because they make the program cryptic and complex.
Syntax
$(Variable_name)
Example
$global_variable = 10
class Class1
def print_global
puts "Global variable in Class1 is #$global_variable"
end
end
class Class2
def print_global
puts "Global variable in Class2 is #$global_variable"
end
end
class1obj = Class1.new
class1obj.print_global
class2obj = Class2.new
class2obj.print_global
Output
Global variable in Class1 is 10
Global variable in Class2 is 10