[前][次][番号順一覧][スレッド一覧]

ruby-changes:26802

From: drbrain <ko1@a...>
Date: Thu, 17 Jan 2013 09:11:31 +0900 (JST)
Subject: [ruby-changes:26802] drbrain:r38854 (trunk): * doc/syntax/control_expressions.rdoc: Added ? : ternary if

drbrain	2013-01-17 09:08:53 +0900 (Thu, 17 Jan 2013)

  New Revision: 38854

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38854

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

  Modified files:
    trunk/ChangeLog
    trunk/doc/syntax/control_expressions.rdoc

Index: doc/syntax/control_expressions.rdoc
===================================================================
--- doc/syntax/control_expressions.rdoc	(revision 38853)
+++ doc/syntax/control_expressions.rdoc	(revision 38854)
@@ -86,6 +86,27 @@ side-effect is to cache a value into a l https://github.com/ruby/ruby/blob/trunk/doc/syntax/control_expressions.rdoc#L86
 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
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38853)
+++ ChangeLog	(revision 38854)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Jan 17 09:08:37 2013  Eric Hodel  <drbrain@s...>
+
+	* doc/syntax/control_expressions.rdoc:  Added ? : ternary if
+
 Thu Jan 17 08:36:04 2013  Eric Hodel  <drbrain@s...>
 
 	* doc/syntax/miscellaneous.rdoc:  Added documentation for alias, undef,

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]