I found ternary operator when tried to use conditional statements inside create()
condition ? expression_if_true : expression_if_false
Simple boolean condition:
x > 0 ? "positive" : "non-positive"
Checking for nil:
result = value.nil? ? "Value is nil" : "Value is not nil"
Nested ternary operators:
grade = score > 90 ? "A" : (score > 80 ? "B" : "C")
Assigning a default value:
name = user_name ? user_name : "Guest"
# or, using the ternary operator
name = user_name || "Guest"
Using ternary in method arguments:
puts x > 0 ? "Positive" : "Non-positive"
Top comments (0)