[RUBY] Things to keep in mind when combining if statements and logical operators

Introduction

There were some points to be aware of when combining the if statement and the logical operator, so I will summarize them.

Basic description

if conditional expression
 #Processing when the conditional expression is true
else
 #Processing when the conditional expression is false
end

Logical operator

&& → and ||→ or

Combination of if statement and logical operator

if conditional expression 1&&Conditional expression 2
 #Processing when conditional expression 1 and conditional expression 2 are true
else
 #Processing when even one of the conditional expressions has false
end

Looking at it in detail, if conditional expression 1 is false, the code of conditional expression 2 is not read in the first place. In particular,


<% if user_signed_in? && @message.user.id == current_user.id %>
<% if @message.user.id == current_user.id && user_signed_in? %>

When using devise to separate the display between logged-in state and non-logged-in state, If you are logged in, neither code will cause an error. However, if you are not logged in, The code above doesn't cause an error, but the code below does.

The reason is that @ message.user.id and current_user.id are not entered unless they are logged in, and it is not possible to determine whether they are equal with == in the first place. On the other hand, in the above formula, if you are not logged in at the time of ʻuser_signed_in?, false is returned, so you do not have to use the code after && `for discrimination.

Recommended Posts

Things to keep in mind when combining if statements and logical operators
Things to keep in mind when using if statements
Things to keep in mind when committing to CRuby
Things to keep in mind when adding war to dependency
Things to keep in mind when testing private methods in JUnit
Things to keep in mind when using Sidekiq with Rails
N things to keep in mind when reading "Introduction to Spring" and "Introduction to Spring" in the Reiwa era
Things to keep in mind when installing Jekyll on Windows and loading themes! !! Need tzinfo !!
Things to keep in mind when using Apache PDFBox® with AWS Lambda
Volume that wants to use a lot of logical operators in if statements
11 Corresponds to comparison and logical operators
[Java Bronze] 5 problems to keep in mind
[Comma (,) is strictly prohibited in the address! ] Things to keep in mind when applying for an exam at Pearson VUE
[Ruby] Difficulty in refactoring logical operators (accuracy and readability)
Things to be aware of when writing code in Java
How to write when you want to keep line breaks and output while avoiding XSS in Rails
[For super beginners] The minimum knowledge you want to keep in mind with hashes and symbols
Java if and switch statements
Unable to get resources when using modules in Gradle and IntelliJ
Things to note when using Spring AOP in Jersery resource classes
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
Things to remember and concepts in the Ruby on Rails tutorial