Ruby Variables Tutorial

In this section, we will learn what Variables are and how to use them in Ruby.

What is variable in Ruby? (Defining Variable)

In short, a variable is a name which points to a memory location where data could be stored! Consider a variable as a door to a storage place where you can put things in there temporarily!

In almost every program out there, there is data! For example, a calculator takes numbers as its inputs and runs the desired operation on those numbers (data) and then returns the result to users.

Now the data that we put in the calculator can’t stay on the fly somehow until the calculator program summons it! It needs to be stored in a place temporarily until it is needed!

That temporarily place is called memory (RAM).

Now, in order to store a value in the memory, we use variables! Basically, these variables are our access point to the memory in order to either get a value that is currently in the memory or set a new value to the memory and access it later.

Another way of thinking about variables is to consider them as a label for a data. For example, you have the number 10 in your program and it is needed in a certain area. Now you can temporarily store this value in the memory and create a variable to point to it (or label it if you will). Then, in later time when you needed the value, all you need to do is to call the variable! Your program, then, is smart enough to use the variable name and see the actual value beneath it and use that actual value instead.

Don’t worry if the concept of variables is still confusing! As we go along, you’ll see tons of examples and the concept becomes really easy to understand.

Ruby variable syntax:

In other programming languages, when you want to create a variable, we usually use the keywords `var` or `let` or `const` first and then put the variable name after that. But in the world of Ruby, we don’t need those keywords! We directly use the variable name and then assign a value to it!

For example:

age = 50

Now if you had this statement in a program, Ruby would’ve created a variable named `age`, stored the value 50 in the memory and made the `age` variable to point to that memory location where the value 50 is stored.

Now the `age` variable is representing the value 50. Basically, the label of the value 50 is now `age`. So anywhere in the program (except on the left side of the assignment `=` operator, which we will explain in a bit) that we use the `age` variable, it will be replaced with the value 50.

Identifiers in Ruby

Another name for the variables in Ruby is identifier! So you could say I have an identifier `age` that represents the value 50 or you could say I have a variable named `age` with the current value of 50.

Note: the word `identifier` is also used for other tools in Ruby as well. For example, the name of a method or the name of a class etc. they all are identifiers which identify a section of your program. We will learn more about them in later sections.

Data Types of Variables in Ruby

In other programming languages, variables have a data type! For example, if you assign a whole number like 10 to a variable, then the data type of that variable is considered as `int` which stands for integer. But in the world of Ruby, a variable itself doesn’t have a data type! A variable is merely a pointer to a memory location where a value of any data type could be stored.

For example, a variable could point to an integer value at first, but then it can be reassigned to another value (make it to point to another memory space) and hence represent another data type.

So just remember that although values in Ruby have different data type, a variable itself doesn’t.

Declaration and Initialization of Variables in Ruby

In programming in general, when we create a variable, it is called declaration. And then when we assign a value to that variable for the first time, it is called initialization.

Now in Ruby, a variable only comes to life when we initialize it right away! Basically, in Ruby, declaring a variable and initializing it happens at the same time (at the same statement).

Example: creating Ruby variables

age = 10

name = "John"

lastName = "Doe"

puts age

puts name

puts lastName





In this little program, we have 3 variables named `age`, `name`, `lastName`. The age variable is representing the value 10 (basically the value 10 is stored in the memory and the `age` variable is pointing to that memory space to represent the value). The `name` variable is representing the value `John` which is a string value (in the data type section we will explain more about data types). And the variable `lastName` is pointing to the `Doe` value.

As you can see, the process of creating (declaring) a variable happens when we assign a value to them right away (initialization).

Note: the `puts` is called method. A method is a name that represents a set of instructions and codes that will do something! For example, the `puts` method takes one or more values and sends that value to the output stream (terminal) to be printed. For example, here when we called the `puts name` the value that the `name` variable was pointing to is taken and given to the `puts` method and then this method sent that value to the output stream to be printed. The same process happened for the `age` and `lastName` variables too.

Ruby Variables Naming Conventions

When creating a variable, there are a couple of conventions (commonly used by Ruby developers) and rules that it’s best to follow if you want your code to run properly and to be read by other developers without any sort of confusion:

  • Ruby is case sensitive! That means if you create a variable with the name `age` then you can’t call the variable with the name `AGE` or `Age`! These are different variables from the perspective of Ruby.
  • A variable name can contain letters, numbers and underscores. But the name of a viable cannot start with a number! For example, if we create a variable with the name `1name` we will get error because the variable is starting with a number.
  • Between a variable name, we can’t use space! For example, `full name` is not a variable because there’s a space between the characters of the variable. If you want to create a variable with multiple names, you can either use the camelCase form (the letter of each word starts with a capital letter except for the first word of the variable which is a lowercase letter) or put underscore `_` between each word. The convention in Ruby is to use underscore between words.
  • Don’t use the reserved words that Ruby is already using for other purposes. (These words are explained in the section below).

Ruby Reserved Words

Ruby has a set of words that it uses them for specific purposes! So when creating variables or other stuff like methods and classes etc. don’t use these reserved words as the name for the identifiers:

alias and BEGIN
begin break case
class def defined?
do else elsif
END end ensure
false for if
module next nil
not or redo
rescue retry return
self super then
true undef unles
until when while
yield __FILE__ __LINE__

Ruby Variable Assignment

If you put a variable on the left side of the assignment `=` operator, you’re basically invoking the variable to assign a value to it.

Note that a variable is not limited to just one value!

Basically, after creating a variable, you’re free to reassign values to that variable! Also note that there’s no limit on the type of value we assign to a variable! For example, if the variable at first had an integer value, we can reassign it to a value of a different type, like string or Boolean at later times.

Example: Variable Assignment in Ruby

age = 10

age = “ten”

Note that here the `age variable had the value 10 at first (basically it represented this value) but then we’ve changed the value of the variable to `ten` which is a string value! So after the second assignment, the `age` variable is representing the value `ten` (The old value is replaced with the new one).

Note: we will learn more about the assignment `=` operator in later sections.

Ruby Variable Reading

If you call a variable in any place other than on the left side of the assignment `=` operator, you’re calling the variable to read its current value!

For example, the `puts` method that we mentioned at the beginning of this section takes a value and sends that to the output stream to be printed. So we can easily pass the variable to this method and it will take the value inside that variable and send it to the output stream.

Example: reading variables value in Ruby

nickname = "Jack"

first_name = "John"

first_name = nickname

puts first_name

puts nickname




Look at this statement in the program above:

first_name = nickname

In this statement, we’re reading the value of the `nickname` and assigning it to the `first_name` variable. So when we called the `puts` method on both variables, they both printed the value “Jack” to the output stream.

Note: calling a variable to get its value is not a cut operation! It’s simply passing a reference from one variable to another! So that means both variables will point to the same value!

More to Read:

Ruby global and local variables


Top Technologies