I learned about the ternary operator when creating an application, so I will keep it as a memorandum.
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