HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Ruby provides several common conditional constructs. Here we will explain all conditional statements and modifiers Ruby available.
if conditional [then] code/en. [elsif conditional [then] code/en.]/en. [else code/en.] end
ifexpression for conditional execution. Valuefalseandnilas false, other values are true. Please note, Ruby use elsif, instead of using else if and elif.
If theconditionalis true, then thecode.If theconditionalis not true, else clause specified in thecodeis executed.
We usually omit the reserved word then. If you want to write complete if the formula in a row, then you must be separated by type and conditions of the program blocks. As follows:
if a == 4 then a = 7 end
#!/usr/bin/ruby # -*- coding: UTF-8 -*- x=1 if x > 2 puts "x 大于 2" elsif x <= 2 and x!=0 puts "x 是 1" else puts "无法得知 x 的值" end
Examples of the above output:
x 是 1
code if condition
if the phrase means that when modified only if the conditions on the right of establishment if the left expression execution. That is, ifconditionalis true, then thecode.
#!/usr/bin/ruby $debug=1 print "debug\n" if $debug
Examples of the above output:
debug
unless conditional [then] code [else code ] end
unless the contrary-acting formula and if, that is, if theconditionalis false, then executecode.If theconditionalis true, else clause specified in thecodeis executed.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- x=1 unless x>2 puts "x 小于 2" else puts "x 大于 2" end
The above example output is:
x 小于 2
code unless conditional
If theconditionalis false, executecode.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $var = 1 print "1 -- 这一行输出\n" if $var print "2 -- 这一行不输出\n" unless $var $var = false print "3 -- 这一行输出\n" unless $var
Examples of the above output:
1 -- 这一行输出 3 -- 这一行输出
case expression [when expression [, expression /en.] [then] code ]/en. [else code ] end
caseexpressionof a judge first match, then branch on the match result.
It uses the=== operator to compare whenthe specifiedexpression,if consistent, then the implementation of the contentwhensection.
We usually omit the reserved word then. If you want to write complete when formula in a row, then you must be separated by type and conditions of the program blocks. As follows:
when a == 4 then a = 7 end
therefore:
case expr0 when expr1, expr2 stmt1 when expr3, expr4 stmt2 else stmt3 end
Substantially similar:
_tmp = expr0 if expr1 === _tmp || expr2 === _tmp stmt1 elsif expr3 === _tmp || expr4 === _tmp stmt2 else stmt3 end
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $age = 5 case $age when 0 /en 2 puts "婴儿" when 3 /en 6 puts "小孩" when 7 /en 12 puts "child" when 13 /en 18 puts "少年" else puts "其他年龄段的" end
The above example output is:
小孩
When "expression" part of the case is omitted, the calculation condition when a part of true expression.
foo = false bar = true quu = false case when foo then puts 'foo is true' when bar then puts 'bar is true' when quu then puts 'quu is true' end # 显示 "bar is true"