Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Uninitialized Variables
2.1.
Class Variables
2.2.
Instance Variables
2.3.
Global Variables
2.4.
Local Variables
3.
Variable References in Ruby
4.
Example
5.
Frequently Asked Questions
5.1.
What is a variable reference?
5.2.
How do we call a variable in Ruby?
5.3.
How do we add a variable to a string in Ruby?
5.4.
Do we have to declare variables in Ruby?
5.5.
What are the types of variable scope in Ruby?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

What Are Variable References in Ruby?

Author yuvatimankar
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction 

We have seen in different programming languages that we can declare a variable, and in ruby, we can also do variable reference. A variable is a name for a value to be stored. It's straightforward you take an object in ruby and assign it to a variable. That variable can be used to manipulate the object.

What are variable references in ruby?

As we know, some programming languages, such as C++, and Perl, make instances of an object when we assign them to a variable, and other programming languages, such as Javascript and python, create a link (a reference or a bonding) between an object and variable. 

So to understand what difference these variables and references in ruby make compared to other programming languages, we are writing this article. So stay till the end to understand the concept of variable references in ruby. Let's discuss uninitialized variables.

Uninitialized Variables

Generally, you should initialize or assign values to your variables before using them in expressions. Ruby does, however, occasionally permit the use of variables that haven't yet been initialized. Different types of variables are subject to different rules:

Class Variables

Before being utilized, class variables must always have a value. If you refer to an empty class variable, Ruby throws a ‘NameError’.

Instance Variables

Ruby returns ‘nil’ when you refer to an uninitialized instance variable. However, relying on this behavior is regarded as poor programming. If you run Ruby with the -w option, it will issue a warning about the uninitialized variable.

Global Variables

When Ruby is run with the -w switch, uninitialized global variables behave similarly to uninitialized instance variables in that they evaluate to ‘nil’ but produce a warning.

Local Variables

This case is trickier than the others since local variables don't have a punctuation character as a prefix. Thus, local variable references resemble method invocation expressions in appearance. When a local variable has been assigned, the Ruby interpreter recognizes that it is a variable rather than a method and can return the variable's value. Ruby considers the expression as a method call if there hasn't been an assignment. Ruby raises a ‘NameError’ if no method with that name already exists.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Variable References in Ruby

A variable is a name of a value. Variables are created, and values are assigned to variables by assignment operators. So whenever the variable's name appears anywhere other than the left side of an assignment, it's a variable reference expression evaluated to the variable's value. 

Example:

one = 1.0   	# It is an assignment expression
one         	# This variable reference expression evaluates to 1.0


There are four different sorts of variables in Ruby, variables that begin with $ are global variables, variables that start with @@ and @ are class variables and instance variables, and that start with an underscore or a lowercase letter are known as local variables.

Every time, variables have simple, unqualified names. If a ‘.’ or ‘::’ is present in an expression, then that expression is a method invocation or a reference to a constant. Let's use the case where Math::PI is a reference to both the expression item.price and constant, and it is a call to the method "price" on the variable item's value.

Example

For example, objects are assigned to variables as:

>> greeting = ‘Hello’
=> “Hello”


This code tells Ruby to relate greeting with the string object with the value 'Hello.' In ruby, greeting represents the reference to the string object.
 

Variable References in Ruby
>>greeting.object_id
=> 70101471431160


In ruby, every object has its object id, which can be retrieved by simply calling “#object_id.” 

Now, let's assign a greeting to a new variable:

>> whazzup = greeting
=> "Hello"

>> greeting
=> "Hello"

>> whazzup
=> "Hello"
>> greeting.object_id
=> 70101471431160

>> whazzup.object_id
=> 70101471431160


The situation of the variable now is :

Variable in Ruby

You can see that the #object_id of both the objects are the same as your computer it may differ. This shows that both whazzup and greeting refer to the same string.

Now, we will show both greeting and whazzup are aliases for each other by using any variables to mutate the object:

>> greeting.upcase!
=> "HELLO"

>> greeting
=> "HELLO"

>> whazzup
=> "HELLO"

>> whazzup.concat('!')
=> "HELLO!"

>> greeting
=> "HELLO!"

>> whazzup
=> "HELLO!"

>> greeting.object_id
=> 70101471431160

>> whazzup.object_id
=> 70101471431160


As we can see, the same object is linked to both variables; when one of the variables is changed, the object is mirrored in the other variable. Also, we can see that the object id does not change. Now the situation is :

Linking Object

So this was the example of a variable reference in Ruby with the help of an object.

Frequently Asked Questions

What is a variable reference?

A variable reference is nothing but another name for the existing variables; a variable reference cannot be NULL.

How do we call a variable in Ruby?

For adding a variable to a string in Ruby, we have to enclose the variable with #{} syntax.

How do we add a variable to a string in Ruby?

For adding a variable to a string in Ruby, we have to enclose the variable with #{} syntax.

Do we have to declare variables in Ruby?

Variables are never declared in ruby. Instead, the rule is that a variable must be present in the assignment before it is used.

What are the types of variable scope in Ruby?

The four types of variable scope available in Ruby are local, global, instance, and class. Ruby also has a single constant type. 

Conclusion

This article extensively discussed the introduction of Ruby and variable references in Ruby. We started with a brief introduction to Ruby, variable reference in Ruby. Then we saw some examples in which we have shown an example of a variable reference in ruby.

You can also learn more about ruby from the links given below.

If you want to learn more on such topics, you can see History of Ruby8 reasons why Ruby should be your first languageDocumentation of Ruby, and Ruby vs. Python. You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structure and AlgorithmsCompetitive Programming, Javascript, and System Design

Thank you

Do upvote our blogs if you find them helpful and engaging!

Happy learning!

Previous article
Literals and Keyword Literals in ruby
Next article
Constant References in ruby
Live masterclass