All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is ivar in Ruby / Rails?

Post cover: What is ivar in Ruby / Rails?
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
Like it?React
🧵

This post doesn't have any additions from the author yet.

What is Memoization (examples in Ruby and Ruby on Rails)?
Feb 20, '25 18:16

What is Memoization (examples in Ruby and Ruby on Rails)?

May 23, '23 07:41

What are attr_accessor, attr_reader, and attr_writer in Ruby? What are they used for?

May 23, '23 11:16

How does the has_many through association (many to many) work in Ruby on Rails?

May 24, '23 18:53

What are joins in Ruby on Rails and how does it work?

May 22, '23 16:26

What is the difference between <%, <%=, <%# and -%> in ERB templates (Ruby on Rails)?

Main methods of authentication in API
Oct 19, '25 20:26

Main methods of authentication in API

What are the differences between OAuth 1 and OAuth 2
Oct 19, '25 20:34

What are the differences between OAuth 1 and OAuth 2

What is ORM and why is it needed?
Oct 26, '25 14:00

What is ORM and why is it needed?

What is Row Security in PostgreSQL and why is it important for Rails developers
Oct 4, '25 19:06

What is Row Security in PostgreSQL and why is it important for Rails developers

MCP: a new internet where websites communicate with AI
Nov 4, '25 11:43

MCP: a new internet where websites communicate with AI

Offset vs Cursor Pagination in Rails: what to choose and why
Sep 24, '25 15:22

Offset vs Cursor Pagination in Rails: what to choose and why

Why is TOON better than JSON when working with AI?
Nov 14, '25 15:14

Why is TOON better than JSON when working with AI?

What is NP-complexity?
Sep 16, '25 19:31

What is NP-complexity?

What is factorial complexity?
Sep 16, '25 19:03

What is factorial complexity?

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

What is exponential growth?

What is Bubble Sort (algorithm explanation)?
Sep 16, '25 18:42

What is Bubble Sort (algorithm explanation)?

May 31, '26 23:56

Copilot error - client not supported: bad request: the specified API version is no longer supported.