About the ternary operator

I learned about the ternary operator when creating an application, so I will keep it as a memorandum.

What is a ternary operator?

Operator used when you want to write "if ~ else ~" in one sentence

Example) (The result is the same)

① Describe with if ~ else


if hoge == 3
  "true"
else
  "false"
end

② Use ternary operator


hoge == 3 ? 'true' : 'false'

Used when condition 1 is incorrect


 It seems to be convenient, but it may be difficult to understand if the expression becomes complicated, so it is important to use it properly.
 The Ruby reference lists it with the `` conditional operator'', and the rails documentation lists it with the `` `ternary operator ```.
 When I looked it up, there seems to be no difference.

### Reference article
https://wa3.i-3-i.info/word11653.html


Recommended Posts

About the ternary operator
About the [ruby] operator
Is the ternary operator evil?
Reference ternary operator
About the method
About the package
Try using || instead of the ternary operator
How do you write the ternary operator (? :)
Output about the method # 2
About the StringBuilder class
Commentary: About the interface
About the asset pipeline
About the function double-java
About the length method
About the Kernel module
About the authenticate method.
About the map method
About the ancestors method
[Output] About the database
About the to_s method.
About the handling of Null
About specifying the JAXRS path
Output about the method Part 1
A note about the scope
About the description of Docker-compose.yml
About the File :: Stat class
About the same and equivalent
About the Android life cycle
About the explanation about functional type
About the programming language Crystal
Consideration about the times method
A confused story about a ternary operator with multiple conditional expressions
About the language to be learned
About the basics of Android development
[Rails] About the Punk List function
About the equals () and hashcode () methods
About the symbol <%%> in Rails erb
About the information sharing app band
About the current development environment (Java 8)
How to write a ternary operator
About Ruby product operator (&) and sum operator (|)
A murmur about the utility class
About the relationship between the Java String equality operator (==) and initialization. Beginners
About the role of the initialize method
Think about the 7 rules of Optional
About =
Summary about the introduction of Device
About the log level of java.util.logging.Logger
[Ruby] I tried to diet the if statement code with the ternary operator
About the version of Docker's Node.js image
Guess about the 2017 Java Persistence Framework (3) Reladomo
Check once again about the OpenSSL command
What is testing? ・ About the importance of testing
About the procedure for java to work
About the operation of next () and nextLine ()
About the initial display of Spring Framework
About the error message Invalid redeclaration of'***'
About the treatment of BigDecimal (with reflection)
About the difference between irb and pry
[Japanese version Faker] About the gem "gimei"
About the number of threads of Completable Future