[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator

Assignment operator

The assignment operator is an operator for assigning a value to a variable. The article below explains it in an easy-to-understand manner, so if you don't understand it, please read it.

https://wa3.i-3-i.info/word18049.html

There are various types of assignment operators, such as:

symbol meaning
= Substitution
+= Add and substitute
-= Subtract and substitute
*= Multiply and substitute
/= Divide and substitute
%= Modulo and substitute
**= Exponentiation and substitution

「||="(Or equal) assignment operator

In the assignment operator,「||=」Such assignment operators also exist.

「="When"||Is a combined operator.

" = " Means assignment. As mentioned above, it is a kind of assignment operator.

python


a = b
Substitute b for a.

continue,「||」Is an operator that returns true if either condition is met. (Meaning OR.) This is called the ** logical operator **. When used with an if statement, it looks like this:

python


if a || b #Returns true if a or b is true and true.
 #If a or b is true
else
 #If both are false
end

What will happen if these two operators are combined?

python


a ||= b
Substitute b if a is false or undefined.
Also a= (a || b)Has the same meaning as.

It will behave like this. The actual example sentences are shown below.

python


  def current_user
    @current_user ||= User.find_by(id: session[:user_id])
  end

If the variable @ current_user exists, it returns the originally existing @ current_user.

If the variable @ current_user does not exist, find the user inUser.find_by (id: session [: user_id])and assign it to the variable @ current_user.

The above code can also be rewritten as follows.

python


  def current_user
    @current_user = @current_user || User.find_by(id: session[:user_id])
  end

References

Rails tutorial Chapter 8 https://railstutorial.jp/chapters/log_in_log_out?version=4.2#cha-log_in_log_out

Recommended Posts

[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] How to use session method
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[Java] [Maven3] Summary of how to use Maven3
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
[Ruby on Rails] How to install Bootstrap in Rails
How to solve the local environment construction of Ruby on Rails (MAC)!
[For Rails beginners] Summary of how to use RSpec (get an overview)
[Ruby On Rails] How to search the contents of params using include?
[Ruby on Rails] How to make the link destination part of the specified id
Method summary to update multiple columns [Ruby on Rails]
[Ruby on Rails] How to write enum in Japanese
[Rails] How to use enum
How to use Ruby return
[Ruby on Rails] How to change the column name
[Rails] How to use enum
[Ruby On Rails] How to reset DB in Heroku
Summary of Java communication API (1) How to use Socket
Ruby: How to use cookies
Summary of Java communication API (3) How to use SocketChannel
[Rails] How to use validation
Summary of Java communication API (2) How to use HttpUrlConnection
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
(Ruby on Rails6) How to create models and tables
Ruby on Rails validation summary
[Rails] How to use Scope
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
Ruby on Rails for beginners! !! Summary of new posting functions
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
How to display a graph in Ruby on Rails (LazyHighChart)
How to deploy a Rails application on AWS (article summary)
[Rails] How to use gem "devise"
Ruby on Rails Overview (Beginner Summary)
How to deploy jQuery on Rails
[Rails] How to use devise (Note)
[Rails] How to use flash messages
Ruby on Rails variable, constant summary
Basic knowledge of Ruby on Rails
How to deploy Bootstrap on Rails
How to use setDefaultCloseOperation () of JFrame
How to use Bio-Formats on Ubuntu 20.04
[Ruby] How to use any? Method
Rails on Tiles (how to write)
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use Ruby inject method
[Ruby On Rails] How to search and save the data of the parent table from the child table
[Ruby] Conditional expression without if? Meaning of (question mark). How to use the ternary operator.
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
[Ruby on Rails] Use the resources method to automatically create routes.
[Ruby on Rails] How to avoid creating unnecessary routes for devise
[Ruby on Rails] Introduction of initial data
A memorandum on how to use Eclipse
[Rails] Addition of Ruby On Rails comment function