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

What is ivar in Ruby / Rails?

ivar in Ruby is an instance variable, denoted by @. In Rails, it passes data from the controller to the view, maintaining the object's state within the request.

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 a rapid increase in magnitude, where each subsequent step multiplies the re...

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

What is factorial complexity?

Factorial complexity is the rapid increase in the number of options, where for n elements the pos...

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

What is NP-complexity?

NP-completeness is a class of problems where finding a solution is extremely difficult, but verif...

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

Main methods of authentication in API

The main methods of authentication in the API on Ruby on Rails: Basic Auth, Token, JWT, and OAuth...

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

What is ORM and why is it needed?

ORM is a technology that allows working with databases through code objects, simplifying developm...