Variables in Ruby are storage locations where data values are kept.
The below example shows how to add two numbers using variables :
x = 5; // Here x is a variable. y = 8; // Here y is also a variable. z = x + y
puts "The added value is : #{z}"
In the above example we are telling Ruby, how to add two numbers(Just like the Maths teacher used to teach a kid).
x = 5
We are asking Ruby to write the number 5 to a variable x.
y = 8
Similarly, we are asking Ruby to write the number 8 to a variable y.
z = x + y
Then, we are adding the values inside x variable and y variable using + operator and putting the added value in z variable using the Assignment Operator =.
puts "The added value is : #{z}"
Finally, we are using the above line to print the added value to the screen.
The sentence within double quotes(i.e. "The added value is : #{z}") as it is, except #{z}.
z is a variable which is holding the value 13. In order to print the value (i.e. 13), we have used #{z}, which is converting the variable z to a String.
And the output would be :
The added value is : 13
Let us say, we want to store the String Hello World in a Variable(Say x).
And the process of storing a String is same as storing a number, but with a mild difference.
Below is the Ruby Code :
x = "Hello World" puts x
So, in the above code, we have initialised the String Hello World to the variable x.
x = "Hello World"
The only thing to note here is, if you are initialising a String (i.e. Hello World), it should be either in Double Quotes ("") or Single Quotes (). i.e. "Hello World" or Hello World.
The above line,
x = "Hello World"
Can also be written as,
x = 'Hello World'
Ruby also gives us the flexibility to assign one value to multiple variables.
Below is the Ruby Code :
x = y = z = "Hello"; puts x puts y puts z
So, in the above code, we have three variables x, y and z. And we have assigned the string Hello to all the three variables in a single line.
x = y = z = "Hello";
And what happens is, three variables, x, y and z are created and assigned with the value Hello.
Next, let us say we have three variables and also three different values.
Interestingly, Ruby provides us the flexibility to assign three values to all three variables in a single line.
Below is the Ruby Code :
x, y, z = "Hello", "Beautiful", "World" puts x puts y puts z
So, in the above code, we have three variables x, y and z. And we have assigned the string Hello, Beautiful and World to all the three variables in a single line.
x, y, z = "Hello", "Beautiful", "World";
Just like any other language, there is a rule to declare the Variables.
Although, the Variables can be of any names with some restriction.
Below are the rules :
So, if you have two numbers and need to assign them to two Variables. You can either declare the variables as x and y.
x = 5
And
y = 7
But the preferred way to name the Variables would be first_number and second_number.
first_number = 5
And
second_number = 7
But you can never use Variable names with spaces like first number or variables with a - like first-number.
A very important point to note is = used above is not exactly same as the equalto used in mathematics.
It simply means the value in the right hand side of = will be put to the variable in left hand side (e.g : x = 5). It's called assignment operator in Ruby.