Ruby Instance Variables Tutorial

  • Post author:
  • Post category:Ruby

In this section, we will learn what the instance variable is and how it is being used in Ruby.

Note: we’re assuming you’re already familiar with the Classes and objects in Ruby.

What is instance Variable in Ruby?

An instance variable is a type of variable that we define in the body of a class (within the body of instance methods, to be specific) and they represent the state of objects that are created from the class.

Basically, each object we create from a class will hold its own instance variable.

Note that an instance variable is not accessible using the objects like the way we access instance methods!

But we can use instance methods to access instance variables and get or change their values (which is equal to changing the state of the object itself if we change the value of these variables).

Alright, let’s see how we can create instance variables and then we will continue our discussion on how they work in Ruby.

Ruby Instance Variable Syntax:

@variable_name

Note that in order to create an instance variable, we use the @ symbol in front of the name of a variable.

Also note that we create an instance variable inside the body of an instance method! We can’t create an instance variable within the body of a class (outside of the instance methods) though!

Example: creating an instance variable in Ruby

class Person 

    def person_identity (first_name, last_name, id)
        @first_name = first_name
        @last_name = last_name
        @id = id 
    end

    def print_full_name 
        puts "The full name is: #{@first_name} #{@last_name} and the person's id is: #{@id}"
    end 
end 

prs = Person.new 

prs.person_identity("John","Doe",21000)

prs.print_full_name

Output:

The full name is: John Doe and the person's id is: 21000

How does instance variable work in Ruby?

In this example, we’ve created three instance variables in the body of the `person_identity` method with the names of `@first_name`, `@last_name`, and `@id`.

Note that just by creating an instance variable within the body of a method, the object that will be created from the target class won’t know anything about those variables at first!

Basically, as long as we don’t call the methods that have instance variables, it’s like the object does not have any instance variable at all!

It’s when we call a method with instance variables, that’s the time when the instance variables will be added to the created object! After that, we can call other methods of the same object in order to access these instance variables and change or get their values.

So it’s really important to first call the methods that have the instance variables to initialize them and then use other methods that want to access the value of the instance variables.

Note: the reverse will cause an error!

Ruby Instance Variables Are Private

As mentioned before, we can’t access the instance variables directly using objects that are created from a class!

They are essentially private from the outside world!

One of the reasons why instance variables are private is because they represent the state of an object and we might not want let objects to directly change these states! For example, we might want to first observe the input value, see if it’s a qualified value, and in that case, only update the target instance variable with the new value. And this is possible only if the input value is passed through a method (which we can then observe the value and decide whether the value can be assigned to the target instance variable using conditional statements like if, unless, etc.).

Example: trying to access an instance variable using an object

class Person 

    def person_identity (first_name, last_name, id)
        @first_name = first_name
        @last_name = last_name
        @id = id 
    end

    def print_full_name 
        puts "The full name is: #{@first_name} #{@last_name} and the person's id is: #{@id}"
    end 
end 

prs = Person.new 

[email protected]_name = "John"

Output:

syntax error, unexpected instance variable

[email protected]_name = "John"

As you can see, because we tried to access the instance variable `@first_name` using the `prs` object, we got an error!

Leave a Reply