Ruby Variables

Ruby Variables
Techiio-author
Written by Sagar RabidasDecember 9, 2021
11 min read
Ruby
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Ruby Variables.

Ruby:-

Ruby is an open-source object-oriented scripting language invented in the mid-90s by Yukihiro Matsumoto.

Unlike languages such as C and C++, a scripting language doesn’t talk directly to hardware. It’s written to a text file and then parsed by an interpreter and turned into code. These programs are generally procedural, meaning they are read from top to bottom.

Object-oriented languages, on the other hand, break out pieces of code into objects that can be created and used as needed. You can reuse these objects in other parts of the program or even other applications.

blogpost

Variables:-

Variables in Ruby are the reminiscence location in which we keep the statistics, and these facts might be utilized by ruby developers whilst needed. In ruby, it helps 5 forms of statistics they may be worldwide variable begins with $, the global variable is to be had for all and its fee can be nil; by using the default, use global variables best if required otherwise keep away from the use of it, example variable starts with @ and having scope as much as particular times, elegance variable, neighborhood variables having scope up to elegance, module and def. And consistent variables start with upper case and can not reassign or alter.

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 which are available for all the scope, but global variables are not secure to use, so we need to make sure 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 in Ruby:

  • Global Variable

Global variables are 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
  • Instance Variables

Instance variables start with @ symbol. Example for instance variables are 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
  • 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()

  • 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 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

ruby
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Sagar Rabidas
Software Developer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+8 more
300 Blog Posts
14 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance