Updated March 30, 2023
Introduction to Ruby Variables
Variables in Ruby are the memory location where we store the data, and these data will be used by ruby developers when needed. In ruby it supports 5 types of data they are global variable(begin with $, the global variable are available for all and its value will be nil; by default, use global variables only if required otherwise avoid using it), instance variable (begin with @ and having scope up to particular instances), class variable (begin with @@), Local variable (Local variables having scope upto class, module and def )and constant variable(start with upper case and can not reassign or modify).
How to Declare & Initialize Variables in Ruby?
Declaration and initialization for the Ruby variable can be done in the below formats.
- For Global Variable: Global variables are the variables that are available for all the scope, but global variables are not secure to use, so we need to make sure of the requirement while using them.
Global variables start with dollar sign like.
$ruby_global_variable = 15
- For Instance Variables: Instance variables can be initialized with @ symbol and the default value for it will be nil.
@ruby_instance_variable = 15
- For Class Variables: Class variables are initialized with @@ and will be defined inside the class.
@@ruby_class_variables = 15
- For Local Variables: Ruby local variables are defined with _ and their scopes are range from def,method to classes.
_ruby_local_variables = 15
- For Constant Variables: Constant variables in ruby are in upper case letters and can not be re-assigned once assigned the value.
CONSTANT1 =15
CONSTANT2 =20
Types of Variables in Ruby
Below are the types of Variables:
1. Global Variable
Global variables start with a dollar($) symbol and contain nil value by default. It is not advisable to use the global variable in all cases.
Example:
In the below example we have defined a variable $ruby_global and it can be accessed inside the two classes called RubyClass1 and RubyClass2. As both, the classes contain a function print_ruby_global which we are on instances of classes. Please follow the below example along with the screen of outputs.
Code:
$ruby_global_variable = 11
class RubyClass1
def print_ruby_global
puts "RubyClass1 global variable output is #$ruby_global_variable"
end
end
class RubyClass2
def print_ruby_global
puts "RubyClass2 global variable output is #$ruby_global_variable"
end
end
rubyclass1obj = RubyClass1.new
rubyclass1obj.print_ruby_global
rubyclass2obj = RubyClass2.new
rubyclass2obj.print_ruby_global
Output:
2. Instance Variables
Instance variables start with @ symbol. Examples for instance variables is given below.
Example:
Below example can be explained in the following steps:
- We have defined a class with the name UserClass and inside this class, we have initialized the three variables.
- Inside this class, we have defined a function called show_detail.
- We finally created objects from the UserClass and with the initialized object(instance) we are calling the function show_detail. And the output for each attribute of the user will be visible to us.
Code:
class UserClass
def initialize(user_id, user_name, address)
@user_id = user_id
@user_name = user_name
@user_addr = address
end
def show_details()
puts "user id is #@user_id"
puts "name of the User is #@user_name"
puts "User address is #@user_addr"
end
end
#
user1 = UserClass.new("1", "Ranjan", "Mount View Apartment guindy, Chennai")
user2 = UserClass.new("2", "Ajay", "B-9 Dhanbad, Jharkhand")
user3 = UserClass.new("2", "Sujoy", "T nagar, Chennai")
user4 = UserClass.new("2", "Vijay", "New ashok nagar, Delhi")
# Call the Methods of class for displaying the details
user1.show_details()
user2.show_details()
user3.show_details
user4.show_details
Output:
3. Class Variables
Class variables can be defined with the @@ symbol. If someone will override the class variable then it will show a warning. Below example can be explained in the following steps:
- First, we defined a class with the name User, this class contains the initializations for the three variables.
- Next, we defined a function called show_details, this function contains the logic to display the details of the users.
- Next, we have created the function for giving the user count, each time the function will get called from the User object and its value will get updated and display the updated value.
Please follow the below example along with the output of the screen.
Code:
class User
@@no_of_users = 0
def initialize(u_id, u_name, u_address)
@user_id = u_id
@user_name = u_name
@user_addr = u_address
end
def show_details()
puts "User ID is #@user_id"
puts "User name is #@user_name"
puts "User Address is #@user_addr"
end
def total_users()
@@no_of_users += 1
puts "Count for the total number of users is: #@@no_of_users"
end
end
user1 = User.new("1", "Ranjan", "Mount View Apartment guindy, Chennai")
user2 = User.new("2", "Ajay", "B-9 Dhanbad, Jharkhand")
user3 =User.new("2", "Sujoy", "T nagar, Chennai")
user4 =User.new("2", "Vijay", "New ashok nagar, Delhi")
# Call the Methods of class for displaying the details
user1.total_users()
user2.total_users()
user3.total_users()
user4.total_users()
Output:
4. Constant Variables
Constant variables start with the upper case letter. The constant variables can not reassign their values. Constants defined inside the class can be accessed inside the class and those defined inside the modules can be accessed inside the modules, we can also define the constant as the global which is outside the class and module and will be available for all. An example of the constant variable is given below.
Explanation:
- First, we have created a class with the name Example For Constant and this class contains two variables (CONSTANT1 and CONSTANT2).
- We created a function inside the class Example For Constant with name display_constant and this function contains the logic to display both constant values.
Example:
Code:
class ExampleForConstant
CONSTANT1 = 101
CONSTANT2 = 201
def display_constant
puts "constant first value is #{CONSTANT1}"
puts "constant second value is #{CONSTANT2}"
end
end
# create an object and call the method display_constant.
object = ExampleForConstant.new()
object.display_constant
Output:
Conclusion
From this tutorial we learned about Ruby variables along with the available constant into it, we learned that there are mainly five types of variables they are global, local, instance, constant and class variable, we also learn the way to use each type of these variables.
Recommended Article
We hope that this EDUCBA information on “Ruby Variables” was beneficial to you. You can view EDUCBA’s recommended articles for more information.