Ruby conditional judgment
Ruby provides several very common conditional structures. Here, we will explain all the conditional statements and modifiers available in Ruby.
Ruby if...else Statement
Syntax
if conditional [then] code... [elsif conditional [then] code...]... [else code...] end
if Expressions are used for conditional execution. The values false and nil are false, and all other values are true. Note that Ruby uses elsif, not else if and elif.
If conditional is true, code is executed. If conditional is not true, the code specified in the else clause is executed.
Usually we omit the reserved word then. If you want to write the complete if formula in one line, you must separate the conditional formula and the program block with then. As shown below:
if a == 4 then a = 7 end
Example
#!/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
The above example output result:
x 是 1
Ruby if Modifier
Grammar
code if condition
The if modifier phrase means that the formula on the left side of if will only be executed when the condition on the right side of if is true. That is, if conditional is true, execute code.
Example
#!/usr/bin/ruby $debug=1 print "debug\n" if $debug
The above example output result:
debug
Ruby unless Statement
Grammar
unless conditional [then] code [else code ] end
unless expression and if expression have the opposite effect, that is, if conditional is false, code will be executed. If conditional is true, execute the code specified in the else clause.
Example
#!/usr/bin/ruby # -*- coding: UTF-8 -*- x=1 unless x>2 puts "x 小于 2" else puts "x 大于 2" end
The output result of the above example is:
x 小于 2
Ruby unless Modifier
Syntax
code unless conditional
If conditional is false, code is executed.
Example
#!/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
The above example output result:
1 -- 这一行输出 3 -- 这一行输出
Ruby case Statement
Syntax
case expression [when expression [, expression ...] [then] code ]... [else code ] end
case first performs a matching judgment on a expression, and then performs branch selection based on the matching result.
It uses the === operator to compare the expression specified by when. If they are consistent, the when part is executed. Content.
Usually we omit the reserved word then. If you want to write the complete when expression in one line, you must separate the conditional expression and the program block with then. It looks like this:
when a == 4 then a = 7 end
So:
case expr0 when expr1, expr2 stmt1 when expr3, expr4 stmt2 else stmt3 end
is basically similar to:
_tmp = expr0 if expr1 === _tmp || expr2 === _tmp stmt1 elsif expr3 === _tmp || expr4 === _tmp stmt2 else stmt3 end
Instance
#!/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
Above The example output result is:
小孩
When the "expression" part of the case is omitted, the first when condition part is true expression will be calculated.
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"