The ternary operator is like a mini if/else statement.
age = 10 type = age < 18 ? "child" : "adult" puts "You are a " + type
The second line contains the ternary operator.
The structure is as follows:
<condition> ? <result if condition is true> : <result if condition is false>
Consider an alternative:
age = 10 type = 'child' if age < 18 type = 'adult' unless age < 18 puts "You are a " + type
Another alternative is to use the multiline if/else option:
age = 10 if age < 18 # w w w.j ava 2 s. c o m type = 'child' else type = 'adult' end puts "You are a " + type
Consider this even simpler version of the first example from this section:
age = 10 puts "You are a " + (age < 18 ? "child" : "adult")