Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - VARIABLES


Variables in Ruby are storage locations where data values are kept.

Example :

The way a kid uses notebook to write numbers for addition, subtraction e.t.c. Similarly, Ruby uses variables to write numbers for addition, subtraction and other purposes.

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}"

Output :



  The added value is : 13

In the above example we are telling Ruby, how to add two numbers(Just like the Maths teacher used to teach a kid).



  1. x = 5

    We are asking Ruby to write the number 5 to a variable x.

    java_Collections

  2. y = 8

    Similarly, we are asking Ruby to write the number 8 to a variable y.

    java_Collections

  3. 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 =.

    java_Collections

  4. 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

Storing a String in a Variable


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 :


Example :



x = "Hello World"
puts x


Output :



  Hello World

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.

java_Collections

The above line,


x = "Hello World"

Can also be written as,


x = 'Hello World'

Dealing with multiple variable and data


Ruby also gives us the flexibility to assign one value to multiple variables.


Below is the Ruby Code :


Example :



x = y = z = "Hello";
puts x
puts y
puts z


Output :



  Hello
  Hello
  Hello

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.

java_Collections

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 :


Example :



x, y, z = "Hello", "Beautiful", "World"
puts x
puts y
puts z


Output :



  Hello
  Beautiful
  World

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";
java_Collections


Ruby Variable Naming Convention


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 :

  1. A lower case Variable is different from an upper case Variable.

    i.e. The lower case variable a and upper case variable A are different.

  2. A Variable should never start with a number.

    i.e. The Variable with name 2y is never allowed.

  3. A Variable can contain alphabets (i.e. A to Z or a to z), numbers (i.e. 0 to 9) or an underscore (i.e. _).

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.


Assignment Operator


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.


Types of Variables in Ruby :

  1. Global Variables : Global variables begins with a $, and is accessible throughout the Programme.

  2. Local Variables : Local variables begins with a _ or a lowercase letter and is accessible only inside a block. We will be learning more about it in the coming tutorials.

  3. Class Variables : Class Variables begins with @@. We will be learning more about it in the coming tutorials.

  4. Instance Variables : Instance Variables begins with @. We will be learning more about it in the coming tutorials.
    Note : We will be explaining Global, Local, Class and Instance variables in detail in comingtutorials.