Skip to content

Commit

Permalink
* doc/syntax/control_expressions.rdoc: Added ? : ternary if
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38854 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
drbrain committed Jan 17, 2013
1 parent 1eb9f71 commit 1be8ac5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Thu Jan 17 09:08:37 2013 Eric Hodel <[email protected]>

* doc/syntax/control_expressions.rdoc: Added ? : ternary if

Thu Jan 17 08:36:04 2013 Eric Hodel <[email protected]>

* doc/syntax/miscellaneous.rdoc: Added documentation for alias, undef,
Expand Down
21 changes: 21 additions & 0 deletions doc/syntax/control_expressions.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1be8ac5

Please sign in to comment.