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

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

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

What is Bubble Sort (algorithm explanation)?

meme code
meme code@memecode
What is exponential growth?
16 Sep 18:57

What is exponential growth?

meme code
meme code@memecode
What is factorial complexity?
16 Sep 19:03

What is factorial complexity?

meme code
meme code@memecode
What is NP-complexity?
16 Sep 19:31

What is NP-complexity?

meme code
meme code@memecode
Offset vs Cursor Pagination in Rails: what to choose and why
24 Sep 15:22

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

meme code
meme code@memecode
What is Row Security in PostgreSQL and why is it important for Rails developers
04 Oct 19:06

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

meme code
meme code@memecode
Main methods of authentication in API
19 Oct 20:26

Main methods of authentication in API

meme code
meme code@memecode
What are the differences between OAuth 1 and OAuth 2
19 Oct 20:34

What are the differences between OAuth 1 and OAuth 2

meme code
meme code@memecode
What is ORM and why is it needed?
26 Oct 14:00

What is ORM and why is it needed?

meme code
meme code@memecode
MCP: a new internet where websites communicate with AI
04 Nov 11:43

MCP: a new internet where websites communicate with AI

meme code
meme code@memecode
Why is TOON better than JSON when working with AI?
14 Nov 15:14

Why is TOON better than JSON when working with AI?

meme code
meme code@memecode