Ruby has a ternary operator (?:
), which returns one of two value based on if a condition evaluates as truthy:
conditional ? value_if_truthy : value_if_falsy
value = true
value ? "true" : "false"
#=> "true"
value = false
value ? "true" : "false"
#=> "false"
it is the same as writing if a then b else c end
, though the ternary is preferred
Examples:
puts (if 1 then 2 else 3 end) # => 2
puts 1 ? 2 : 3 # => 2
x = if 1 then 2 else 3 end
puts x # => 2