Variable Scope and Variable Types in Ruby
In this lesson, we are going to talk about the five different types of variables in Ruby including how and when they should be used, along with how variable scope functions in the Ruby programming language.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this lesson, we are going to talk about the five different types of variables in Ruby, including how and when they should be used along with how variable scope works in the Ruby programming language. For this lesson, I will be using the Sublime Text editor instead of repl.it so that you can see how Ruby works using files (which is the standard way to build scripts).

The five different Ruby variable types are:

  • Local variables
  • Global variables
  • Instance variables
  • Constants
  • Class variables

Let's take a look at each of these variables types in detail.

Local variables

Local variables are variables whose scope is limited to the area where it is declared. In other words, if a local variable is declared inside a method or a loop, then its scope is limited to that method and loop respectively.

For example, I am declaring a variable called x and asking it to be printed 10 times inside a loop. This is how it will look:

10.times do
  x = 10
  p x
end

You can now go to the terminal and run this program. Before that, first check if you're in the right directory by typing the command ls and this will show the files in that directory. In this case, I've named my file as variable_types.rb and I can find this file in the list, and this means I'm in the right place.

Now, to check this code you need to run the file, and you can do this with the command:

ruby variable_types.rb

This will print the value 10 10 times for you.

Now, what happens when you try to print the value of the variable x outside the loop. Logically, you would think, x would have the value of 10, and this would be printed only once since it's outside the loop.

10.times do
  x = 10
end

p x

However, when your run the program, it throws an error.

large

This error is because x is a local variable that works only inside the loop, and its value is unknown outside the loop. In other words, the system cannot tell you the value of x because it is not available outside the loop.

So, this is the scope of local variables.

Global variables

A global variable is a variable that is available for the entire application to use, and this is denoted by a $ preceding the variable. Now, if you apply the same code, you can see that x is printed. This is how the code should look like.

10.times do
  $x = 10
end

p $x

Now, if you run it in the console, it prints a value of 10.

Though this looks easy, using global variables is not a good idea. In my entire life, I think I've probably used a global variable only once, and now thinking back, there could have been a better way to do it. In general, It's not used because it is hard to track the value of these variables.

Let me give you a scenario. Let's create two files, namely, File 1 and File 2. In File 1, let's assume we have a long algorithm and I create a global variable as a part of this algorithm. For presentation purposes, this is a baseball game and I have my global variable set to Yankees.

$global_var = "Yankees"

Say, another developer who is working on this baseball application with me is working on File 2. As a part of his algorithm, he also creates a global variable, but sets it to Astros. So, in File 2, we have this code:

$global_var = "Astros"

So, when someone runs this program, the last file that gets loaded sets the value for this global variable. Also, the developer who created File 1 has no idea that the value of global variable was altered to Astros in another file. So, the output will not be what he expects and overall, it can lead to a lot of confusion. This is why it's always a good idea to use variables that have limited scope, such as local or instance variables.

Instance variables

As the name suggests, instance variables are available to a particular instance. There is a specific syntax to set instance variables, you need to use the @ sign to define a variable. For example, keeping in tune with our baseball theme, we can set the batting average like this:

@batting_average = 300

I'm going to show you how instance variables are used in a real life Ruby on Rails application.

large

In this code, you can see that there is an instance variable called @jobs. This variable is created in the method index and is not available to other methods in the file. Now, you may wonder why we wouldn't just make this a local variable since it's not available to other methods in the class.

The reason for this is because Rails is structured in such a way that the view and controller files are wired to communicate with each other, so this instance variable @jobs can be accessed in the associated view file. This is how the view file looks like:

large

Now @jobs is available for the view page only because we made it an instance variable in the controller file. devCamp Note: Don't worry if this sounds foreign to you. This is a more advanced development topic and I'm introducing it here so it will look familiar to you when going through Rails applications. For now I just want you to understand the scope of an instance variable and how it is different from local variables.

Constants

If you're coming from other programming languages, Ruby handles constants differently than what you may be used to. Constants, in general, take values that do not change through the entire application. The syntax is to use all capital letters while naming your constant so that the application knows how to handle it. For example, to set a constant to hold a baseball team you would declare it this way:

TEAM = "Angels"

Typically other programming languages will not allow you to change the value of TEAM. However, Ruby does not hold you back, and takes the last value assigned to the constant. In the above example, I can change its value to:

TEAM = "Athletics"

Other programming languages would either throw an error or would print the value Angels. However, Ruby prints the value Athletics because that is the last value assigned to the variable TEAM. Also, it gives a warning message that says that the constant was already initialized and was changed because changing a constant is considered a poor programming practice. But, it still lets you do to make that change and follows the Ruby convention of trusting the developer to make the right programming decision. So, be careful while using constants in Ruby since they can be overridden.

Class variables

Class variables are variables that are available to a particular class. The syntax for this variable is @@. For example,

class MyClass
  @@teams = ["A's", "Tigers"]
end

Though it looks simple, you'll rarely use class variables in real-world applications because you can accomplish the same through local or instance variables. Nothing is wrong if you use class variables, but it's not commonly utilized by most developers. In fact, the local and instance variables are likely to make up more than 98 percent of variables in your applications, so it's a good idea to be familiar with them.