Changes

Jump to: navigation, search

Ruby Flow Control

657 bytes added, 21:12, 19 November 2007
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>

Navigation menu