← homeProgramming (Програмування)

What is ivar in Ruby / Rails?

ivar is short for instance variable. In Ruby, it is written with a @ before the name, for example: @user = User.find(params[:id]) How does it work? In Ruby, each object has its own set of instance variables. That is, ...

This content has been automatically translated from Ukrainian.
ivar is short for instance variable.
In Ruby, it is written with a @ before the name, for example:
@user = User.find(params[:id])
How does it work?
In Ruby, each object has its own set of instance variables. That is, @user in the controller and @user in the view are the same variable within a single request, but it belongs to a specific instance of the controller.
Rails automatically makes all @-variables from the controller available in the corresponding template.
controller:
# app/controllers/users_controller.rb
def show
  @user = User.find(params[:id])
end
and the same view:
<!-- app/views/users/show.html.erb -->
<h1><%= @user.name %></h1>
In short:
  • @ivar = instance variable = instance variable of an object
  • Visibility within a single instance
  • Rails automatically passes @-variables from the controller to the view
  • Used to pass data between MVC layers

🔥 More posts

All posts
What is exponential growth?
Sep 16, '25 18:57

What is exponential growth?

Exponential growth is the process where a quantity increases in a geometric progression. In other...

What is factorial complexity?
Programming (Програмування)Sep 16, '25 19:03

What is factorial complexity?

Factorial complexity is a situation where the number of options or combinations grows like the fa...

What is NP-complexity?
Programming (Програмування)Sep 16, '25 19:31

What is NP-complexity?

NP-completeness is a class of problems for which it is very difficult to find a solution, but eas...

Main methods of authentication in API
Programming (Програмування)Oct 19, '25 20:26

Main methods of authentication in API

When we create an API in Ruby on Rails, it is important to control who has access to resources. H...

What is ORM and why is it needed?
Programming (Програмування)Oct 26, '25 14:00

What is ORM and why is it needed?

When we work with databases, we usually have to write SQL queries - selections, inserts, updates,...