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 (instance variable).
In Ruby, she records with @ before the title, 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 controllers and @user in views - it's the same variable within one request, but she belongs to a specific instance of the controller.
Rails automatically makes all @-variables from the controller available in the appropriate template.
controller:
# app/controllers/users_controller.rb
def show
  @user = User.find(params[:id])
end
same puff:
<!-- app/views/users/show.html.erb -->
<h1><%= @user.name %></h1>
In short:
  • @ivar = instance variable = object instance variable
  • Visibility within limits one copy
  • Rails automatically transmits @-variables from the controller to view
  • Used to transfer 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 Rails developers
04 Oct 19:06

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

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

Basic methods of authentication in the API

meme code
meme code@memecode
How do OAuth 1 differ from OAuth 2
19 Oct 20:34

How do OAuth 1 differ from 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 sites communicate with AI
04 Nov 11:43

MCP: A new internet where sites 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