Ruby/Language Basics/Ternary Operator
Материал из Wiki.crossplatform.ru
Содержание[убрать] |
Consider an alternative of Ternary Operator
age = 10 type = "child" if age < 18 type = "adult" unless age < 18 puts "You are a " + type
The ternary operator can be used to build expressions on a single line
age = 10 puts "You are a " + (age < 18 ? "child" : "adult")
The Ternary Operator: <condition> ? <result if condition is true> : <result if condition is false>
age = 10 type = age < 18 ? "child" : "adult" puts "You are a " + type
Using the Ternary Operator
# result = condition ? true_value : false_value toppings = 4 price = toppings > 3 ? 5.99 : 4.99