Introduction
Programming is the act of making something new. When you run a program, it creates a miniature universe. Things come to life and interact with one another according to the rules you establish as a creator.
Ruby is an object-oriented programming language, like C++, Java, Javascript, and many other popular programming languages.
Well, What’s for today?
In this article, we are going to discuss the Variables in Ruby in detail.
We want to give names to the "things" (objects) that our program deals with.
Variables are a feature found in every practical programming language. There are various kinds of variables in Ruby (you will learn about the different types in a bit).
Don't worry if some of this seems a little hazy to you.
Variables in Ruby
Ruby variables are storage locations for data that will be used in programs. Each variable has a unique name. These variable names follow naming conventions.
Unlike other programming languages, Ruby does not require variable declaration. To indicate it, a prefix is required.
In short, a variable is just a label. It's a method of naming things in your Ruby programs.
Just like the names we give to real-world objects.
When I say “Mango”, you know what I’m talking about. I don’t have to describe it to you. That’s what variables do!
And they're far more useful than you might imagine.
Creating Variables
Variables are created by associating a Ruby object with a variable name. This is known as "variable assignment."
For example
|
When you type name, Ruby will automatically convert it into “Ninja”. Variables are just names for things.
How to use Variables?
To use a variable, you write its name.
|
In the above example, the name is concatenated with Coder.
We can also combine multiple variables together.
|
We can also store the result of the above-concatenated string into another variable.
|
How Ruby does it?
So, before doing anything else, Ruby will examine the right expression and evaluate it. She will then evaluate the assignment operator =).
Ruby Variable Types
In ruby, we have different variable types.
So far, we have used the Local Variables. Now, let us discuss each type one by one:
Local Variables
The name of a local variable begins with a lowercase letter or an underscore ( _ ). It is only accessible or has its scope within the initialization block. Variable has no scope once the code block is finished.
When uninitialized local variables are called, they are interpreted as a call to an empty method.
Example for Local Variables
|
Instance Variables
The name of an instance variable begins with a @ sign. It belongs to one instance of the class and can be accessed within a method from any instance of the class.
- They only have restricted access to a single instance of a class.
- They are not required to be initialised.
-
A nil value will be assigned to an uninitialized instance variable.
Example for Instance Variables
|
Output
Class Variables
The @@ sign precedes the name of a class variable. They must be initialised before being used. A class variable is shared by the entire class and can be accessed from anywhere within the class. If the value is changed at one instance, it is changed at all instances.
Example for Class Variables
|
Output
Global Variables
The name of a global variable begins with a $ sign. It has a global scope, which means it can be accessed from anywhere in a program.
Uninitialized global variables have no value. It is not recommended to use them because they make programs cryptic and complex.
Example for Global Variables
|
Output