Be absolutely careful when putting the result of and / or in a variable!

[x = true and false] does not work as expected

true and false # => false
false and true # => false
true and true # => true
false and false # => false

true && false # => false
false && true # => false
true && true # => true
false && false # => false

x = true and false # => false
p x # => true !!!!!!!!!!
x = false and true # => false
p x # => false
x = true and true # => true
p x # => true
x = false and false # => false
p x # => false

x = true && false # => false
p x # => false
x = false && true # => false
p x # => false
x = true && true # => true
p x # => true
x = false && false # => false
p x # => false

Reason

https://docs.ruby-lang.org/ja/latest/doc/spec=2foperator.html

Has a lower priority than =, so with x = true and false

  1. x = true
  2. true and false (results are discarded)

Will occur, and x will not become false.

solution

Recommended Posts

Be absolutely careful when putting the result of and / or in a variable!
Be careful with requests and responses when using the Serverless Framework in Java
[Ruby] When you want to assign the result obtained by conditional branching to a variable and put it in the argument
[Rails] Where to be careful in the description of validation
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
Settings to be made when HTML or JSP is formatted in Eclipse and the layout is disappointing
Be careful when omitting return in Ruby
Get the result of POST in Java
The story of forgetting to close a file in Java and failing
How to change the value of a variable at a breakpoint in intelliJ
Be careful of daylight saving time when newly registering / referencing a user's birthday
Be careful about upgrade if you use | etc. in the URL of Tomcat
Measure the size of a folder in Java
[Java] Be careful of the key type of Map
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
A memo about the types of Java O/R mappers and how to select them
Takes out the HashMap value and returns the result sorted by the value of value as a list.
Pass arguments to the method and receive the result of the operation as a return value
Understand in 3 minutes! A very rough explanation of the difference between session and cookie
Count the number of occurrences of a string in Ruby
When deploying EC2, bundle install cannot be performed and an error of Undefined local variable or method `git_source'for Gemfile appears.
Return the execution result of Service class in ServiceResponse class
[For beginners] DI ~ The basics of DI and DI in Spring ~
[Java] When writing the source ... A memorandum of understanding ①
How to convert A to a and a to A using AND and OR in Java
Be careful about Ruby method calls and variable references
[Java10] Be careful of using var and generics together
[Spring Data JPA] Can And condition be used in the automatically implemented method of delete?
When I switched to IntelliJ, I got a lot of differences in the encoding of the properties file.
[Technical memo] Things to be careful of from an engineer's point of view when creating a view
A program (Java) that outputs the sum of odd and even numbers in an array
One of the causes and solutions when whales do not appear in Docker Quickstart Terminal
In SpringBoot 2.3.0 or later, the placeholder used for the value of @PropertySource fails to be resolved.
Memo to silently write the source that seems to be usable and the result of compilation execution-java
A quick explanation of the five types of static in Java
Check the dependency of a specific maven artifact in Coursier
Determine that the value is a multiple of 〇 in Ruby
Be careful of initialization timing when using MessageEncryptor with Rails 5.2 / 6.0
The nth and n + 1st characters of a Ruby string
I tried JAX-RS and made a note of the procedure
Things to be aware of when writing code in Java
A program that counts the number of words in a List
Create more Tabs and Fragments in the Fragment of BottomNavigationView
Be sure to compare the result of Java compareTo with 0
Find the number of days in a month with Kotlin
[Swift] When you want to know if the number of characters in a String matches a certain number ...
A memo of the update command when npm install causes an error in GitHub Action etc.
I tried to express the result of before and after of Date class with a number line
[Be careful about changing the version of Xdebug! ] Create a development environment with Xdebug3 + docker + VS Code
Graph the sensor information of Raspberry Pi in Java and check it with a web browser
Generate a serial number with Hibernate (JPA) TableGenerator and store it in the Id of String.
Solution if you delete the migration file in the up state
[Rails] Where to be careful in the description of validation
[Swift5] How to create a .gitignore file and the code that should be written by default