Ruby/Language Basics/Ternary Operator

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
м (1 версия: Импорт выборки материалов по Ruby)
 

Текущая версия на 17:59, 13 сентября 2010

Содержание

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