Ruby Language Reference
if conditional code elsif conditional code else code end
If statement executes code if the conditional is true. If the conditional is not true, code in the else clause is executed.
traffic = :stop if traffic == :stop p 'The traffic is at stop' else p 'The traffic is flowing' end
The traffic is at stop
If statements are expressions that evaluate to a value, the result can be assigned to a variable:
traffic = :stop status = if traffic == :stop 'stopped' else 'flowing' end p status
If in Statement Modifier
raining = true p 'Take umbrella' if raining
If with Ternary Operators
raining = true status = raining ? 'Take umbrella' : 'No umbrella' p status
If with Elsif Keyword
skill = :advanced level = if skill == :advanced 'Guru' elsif skill == :intermediate 'Journey Man' else 'Newbie' end p level
This prints: 'Guru'.