Ruby Variable
Ruby Variable
Ruby have a four type variable -
1. Local Variable
2. Instance Variable
3. Class Variable
4. Global Variable
1. Local variable
A Local variable name start with lowercase letter(a-z) or underscore (_) . Local variables are not available outside the methods. There is no need to initialize the local variables.
Example
age = 21
_Age = 22
2. Instance variable
An instance variable name starts with a @ sign. It belongs to one instance of the class and can be accessed from any instance of the class within a method. They only have limited access to a particular instance of a class. There is no need to initialize the instance variables and uninitialized instance variable always contains a nil value.
Example
# the Instance Variables Example
class Employee
def initialize(id, name, age)
# Instance Variables
@Emp_id = id
@Emp_name = name
@Emp_age = age
end
# displaying result
def display_details()
puts "The Employee id is #@Emp_id"
puts "The Employee name is #@Emp_name"
puts "The Employee age is #@Emp_age"
end
end
# Create Objects
emp1 = Employee.new("11", "Ram", 21)
emp2 = Employee.new("21", "Shayam", 23)
# Call Methods
emp1.display_details()
emp2.display_details()
output:
The Employee id is 11
The Employee name is Ram
The Employee age is 21
The Employee id is 21
The Employee name is Shayam
The Employee age is 23
3. Class variable
A class variable name start with @@ sign.They need to be initialized before use.A class variable belongs to the whole class and can be accessible from anywhere inside the class. If the value will be changed at one instance, it will be changed at every instance.
Example
# the Class Variables Example
class Employee
# class variable
@@no_of_employees = 0
def initialize(id, name, age, address)
# An instance Variable
@emp_id = id
@emp_name = name
@emp_age = age
@emp_addr = address
end
# printing result
def print_details()
puts "Employee id is #@emp_id"
puts "Employee name is #@emp_name"
puts "Employee age is #@emp_age"
puts "Employee address is #@emp_addr"
end
def total_no_of_employee()
# class variable
@@no_of_employees += 1
puts "Total number of Employees: #@@no_of_employees"
end
end
# Create Objects
emp1 = Employee.new("12", "Shayam Parmar", 21, "Indore, Madhya Pradesh")
emp2 = Employee.new("20", "Rahul Parmar", 24, "Ratlam, Madhya Pradesh")
# Call Methods
emp1.print_details()
emp1.total_no_of_employee()
emp2.print_details()
emp2.total_no_of_employee()
output:
Employee id is: 12
Employee name is: Shayam Parmar
Employee age is: 21
Employee address is: Indore, Madhya Pradesh
Total number of Employees: 1
Employee id is: 20
Employee name is: Rahul Parmar
Employee age is: 24
Employee address is: Ratlam, Madhya Pradesh
Total number of Employees: 2
4. Global variable
A global variable name starts with a $ sign. Its scope is globally, means it can be accessed from any where in a program.Class variables are not available across classes. If you want to have a single variable, which is available across classes, you need to define a global variable.
Example
$global_variable = 10
class Employee
def print_global
puts "Global variable in Employee class is #$global_variable"
end
def initialize(id, name, age)
# Instance Variables
@Emp_id = id
@Emp_name = name
@Emp_age = age
end
# displaying result
def display_details()
puts "The Employee id is #@Emp_id"
puts "The Employee name is #@Emp_name"
puts "The Employee age is #@Emp_age"
end
end
class Two2
def print_global
puts "Global variable in two2 class is #$global_variable"
end
end
Employeeobj = Employee.new("11", "Ram", 21);
Employeeobj.print_global
Employeeobj.display_details()
Two2obj = Two2.new
Two2obj.print_global
Output:
Global variable in Employee is 10
The Employee id is 11
The Employee name is Ram
The Employee age is 21
Global variable in two2 is 10

Comments
Post a Comment