34,333
edits
Changes
→The Ruby Ternary Operator
== The Ruby Ternary Operator ==
Ruby uses something called a ''ternary operator'' to provide a shortcut way of making decisions. The syntax of the ternary operator is as follows:
[condition] ? [true expression]:[false expression]
The way this works is that [condition] is replaced with an expression that will return either true or false. If the result is true then the expression that replaces the [true expression] is evaluated. Conversely, if the result was false then the [false expression] is evaluated. Let's see this in action:
<pre>
irb(main):182:0> customerName = "Fred"
=> "Fred"
irb(main):183:0> customerName == "Fred" ? "Hello Fred" : "Who are you?"
=> "Hello Fred"
</pre>