What Is a Variable?
A variable is a label, or a container used to store data in a Ruby program. Each variable in a program has a unique name and a value that it holds.
Variables in Ruby points to an object. Hence, when assigning a variable, you assign the object referenced by the variable. Each object is a specific data type, either built-in or custom-created.
Variable Naming Conventions in Ruby
When naming a variable, consider following several key points:
- You can only create the name from alphanumeric characters or an underscore.
- The name of a variable cannot begin with a numerical value.
- Names in Ruby are case-sensitive. Thus, the variable Name and name are not similar.
- Variable names cannot begin in an Uppercase letter. If so, Ruby takes the identifier as a constant.
- Variable names cannot contain special characters.
- Use Snake case when naming variables. This means you should separate names with an underscore. For example, students_name.
- Variable name should not be a Ruby Reserved keyword.
The following are examples of incorrect variable names in Ruby:
begin
12th_day
[jdfh]
Although you can name a variable anything you want—as long it obeys the conventions of naming variables—it is better to use descriptive names to make them easy to remember and readable.
Assigning Variables
Once you define a name for your variable in Ruby, you assign its corresponding value using a single equal sign (=).
For example:
As mentioned above, assigning a variable creates a reference to the Ruby object.
Ruby is a dynamic language. Unlike strongly typed languages such as C, C++, Ruby does not require you to declare the variable’s data type.
first_name = "John"
char first_name[] = "John";
In the example above, Ruby does not need you to specify if the type is a string, integer, hash, or other.
The Ruby interpreter determines the type of the variable during the assignment process.
How to Use Ruby Variables
Once you declare and assign a value to a variable, you can use it any way you want. For example, you can perform mathematical operations:
puts age + 20;
output:
30
Also, you can concatenate strings to create a single string as:
last_name = "Doe"
full_name = first_name + " " + last_name
puts full_name
output:
John Doe
To interpolate a variable in a string, you can use the #{} format. For example:
puts "You are #{age} years old"
output:
You are 10 years old
Ruby Variable Scope and Sigils
Throughout the previous examples, we have used a local variable. Local variables are available in the block in which they are declared. For example, if a variable is declared in a loop or a method, it is only accessible inside that scope.
As seen in the examples above, a local variable is declared by a lowercase letter or an underscore.
_also_lower = 20
Ruby also supports other types of variables. These include:
- Global Variables
- Class Variables
- Instance Variables
- Constant Variables
1. Global Variables
Let us start with global variables. We declare them using a preceding dollar sign in the variable name. As the name suggests, a global variable has a global scope. This means the variable is accessible in the entire Ruby program regardless of its declaration location.
Here is an example:
def MethodName
puts $global
end
class ClassName
puts $global
end
module ModuleName
$global = "I am new"
puts $global
end
In this example, the global variable is available in classes, methods, and modules. The value of the global variable is accessible throughout, and it is adjustable at each scope.
Global variables are useful, and the Ruby interpreter has a set of predefined global variables.
2. Class Variables
The other type of variable is class variables. They are defined by appending double @ signs before the variable name. Class variables are shared by the instances of a class. Hence, if an object modifies the value of a class variable, the change reflects on other object instances:
@@wasted = false
def points_calc
points = 10;
end
def game_over?
@@wasted
end
end
instance1 = ClassName.new
instance2 = ClassName.new
instance3 = ClassName.new
The @@wasted variable is a class variable shared between all the instances of the class.
3. Instance Variables
The next variable type is instance variables. We define them by appending a single @ sign before the name. They work similarly to class variables. However, their scope is limited to a specific instance of an object and not all. Thus, if the variable’s value is changed, it only reflects on that particular instance.
4. Constant Variables
Constant variables are variables that, once declared, values should not be changed. They are declared using uppercase letters. However, unlike other programming languages that do not allow the modification of a const variable, Ruby allows it but it will throw a warning.
Constant variables’ scope depends on their level of declaration. For example, if you declare a constant variable outside the scope of a class or method, it takes a global scope.
However, if you declare it inside a class or method, it limits the scope to the class context and method in which they are declared:
VAR = VAR + 10;
puts VAR
zero.rb:3: warning: already initialized constant VAR
zero.rb:1: warning: previous definition of VAR was here
13.141
In the above example, we modify the value of a constant variable. As you will see, Ruby only gives a warning but still changes the value.
Conclusion
This tutorial highlights the definition of a variable and the ways to create and use Ruby variables. In addition, significant factors to consider in the variable naming convention in the Ruby programming language and the different Ruby variable scope and sigils are discussed. I hope you found this tutorial helpful.