Ruby conditional
Ruby provides several common conditional constructs. Here we will explain all conditional statements and modifiers Ruby available.
Rubyif ... else statement
grammar
if conditional [then] code... [elsif conditional [then] code...]... [else code...] 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
Examples
#!/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
Rubyif modifiers
grammar
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.
Examples
#!/usr/bin/ruby $debug=1 print "debug\n" if $debug
Examples of the above output:
debug
Rubyunless the statement
grammar
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.
Examples
#!/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
Rubyunless modifiers
grammar
code unless conditional
If theconditionalis false, executecode.
Examples
#!/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 -- 这一行输出
Rubycase statement
grammar
case expression [when expression [, expression ...] [then] code ]... [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
Examples
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $age = 5 case $age when 0 .. 2 puts "婴儿" when 3 .. 6 puts "小孩" when 7 .. 12 puts "child" when 13 .. 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"