[Ruby] Conditional expression without if? Meaning of (question mark). How to use the ternary operator.

This is a personal memo.

Understand the meaning of expressions that have ? (Hatena) and : (colon) without if, as shown below.

X = y ==" aaa "? V: w

Ternary operator

Expressions using ? (Hatena) and : (colon) are abbreviations for if else statements and are called ternary operators.

The following two processes are the same.

Normal


if conditional expression
Return value 1
else
Return value 1
end

Ternary operator


Conditional expression?Return value 1:Return value 2

Since there are three items, (1) conditional expression, (2) return value 1, and (3) return value 2, it is called a ternary operator.


## Reference operator example If the conditional expression is true, the return value 1 is returned.

python


x = 10
x == 10 ? "Yes" : "No"

=> "Yes"

If the conditional expression is false, the return value 2 is returned.

python


x = 10
x != 10 ? "Yes" : "No"

=> "No"

## `x = y ==" aaa "? V: w` processing content This is the process of assigning the result of the ternary operator to the variable x.

python


y = "aaa"
v = "Yes"
w = "No"

x = y == "aaa" ? v : w

p x
=> "Yes"

Since y ==" aaa " is true, the previous return value v is assigned to x.

Recommended Posts

[Ruby] Conditional expression without if? Meaning of (question mark). How to use the ternary operator.
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Ruby] if statement concept of conditional expression
[Ruby] I tried to diet the if statement code with the ternary operator
Ruby conditional branch. if, conditional operator (ternary operator), unless, case
Output of how to use the slice method
[Ruby basics] How to use the slice method
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
[Ruby] How to use standard output in conditional branching
[Ruby] How to find the sum of each digit
How to use git with the power of jgit in an environment without git commands
How to use Ruby return
[Ruby] How to retrieve the contents of a double hash
How to add elements without specifying the length of the array
Ruby: How to use cookies
How to mark up search keywords regardless of case and without changing the search source word
How to change the contents of the jar file without decompressing
The operator that was born to be born, instanceof (Java) ~ How to use the instanceof operator ~
How to use the link_to method
How to use the include? method
How to use the form_with method
How to use the wrapper class
How to use Ruby on Rails
How to use setDefaultCloseOperation () of JFrame
[Ruby] How to use any? Method
How to write a ternary operator
How to use Ruby inject method
[Introduction to Java] Conditional branching (if statement, if-else statement, else if statement, ternary operator, switch statement)
How to solve the local environment construction of Ruby on Rails (MAC)!
I tried to make full use of the CPU core in Ruby
[Ruby On Rails] How to search the contents of params using include?
[Java] How to use the File class
[Ruby on Rails] How to use CarrierWave
[Java] How to use the hasNext function
[Java] How to use the HashMap class
[Rails] How to use the map method
How do you write the ternary operator (? :)
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
Ruby length, size, count How to use
How to determine the number of parallels
[Ruby] How to use slice for beginners
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to sort the List of SelectItem
[Ruby on Rails] How to use redirect_to
[Processing × Java] How to use the function
[Ruby on Rails] How to use kaminari
[Java] How to use the Calendar class
Ruby: CSV :: How to use Table Note
Use hashes well in Ruby to calculate the total amount of an order
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
How to use the camera module OV7725 (ESP32-WROVER-B)
[Ruby] Behavior of evaluation of conditional expression in while
[Java] How to use Thread.sleep to pause the program
Customize how to divide the contents of Recyclerview
Assignment to multiple variables with the ternary operator