summaryrefslogtreecommitdiff
path: root/doc/syntax
diff options
context:
space:
mode:
authordrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-17 00:08:53 +0000
committerdrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-17 00:08:53 +0000
commit1be8ac57abca95de2525c9b56fb51e9c8a802660 (patch)
tree6dbeaabeec92f0300f8b0656f6e5b6647cdfffe3 /doc/syntax
parent1eb9f71c3ae41182a08cd965340854f116354827 (diff)
* doc/syntax/control_expressions.rdoc: Added ? : ternary if
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38854 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'doc/syntax')
-rw-r--r--doc/syntax/control_expressions.rdoc21
1 files changed, 21 insertions, 0 deletions
diff --git a/doc/syntax/control_expressions.rdoc b/doc/syntax/control_expressions.rdoc
index e3fc19d..0efc166 100644
--- a/doc/syntax/control_expressions.rdoc
+++ b/doc/syntax/control_expressions.rdoc
@@ -86,6 +86,27 @@ side-effect is to cache a value into a local variable:
The result value of an +if+ expression is the last value executed in the
expression.
+== Ternary if
+
+You may also write a if-then-else expression using <code>?</code> and
+<code>:</code>. This ternary if:
+
+ input_type = gets =~ /hello/i ? "greeting" : "other"
+
+Is the same as this +if+ expression:
+
+ input_type =
+ if gets =~ /hello/i
+ "greeting"
+ else
+ "other"
+ end
+
+While the ternary if is much shorter to write than the more verbose form, for
+readability it is recommended that the ternary if is only used for simple
+conditionals. Also, avoid using multiple ternary conditions in the same
+expression as this can be confusing.
+
== +unless+ Expression
The +unless+ expression is the opposite of the +if+ expression. If the value