Ruby provides several common conditional structures. Here, we will explain all the conditional statements and the modifiers available in Ruby. If Usually we omit the reserved words. The output result of the above example: The output result of the above example: The output result of the above example is: If The output result of the above example: It uses the Usually we omit the reserved words Therefore: Basically similar to: The output result of the above example is: When 6.16.1. Ruby
if...else
statement # Grammar #
ifconditional[then]code...[elsifconditional[then]code...]...[elsecode...]end
if
expressions are used for conditional execution. Value
false
and
nil
if false, all other values are true. Note that Ruby uses the
elsif
, not using the
else
if
and
elif
.
conditional
is true, execute
code
. If
conditional
is not true, then execute
else
clause specified in the
code
.
then
. If you want to write a complete
if
type, you must use the
then
separate conditional and program blocks as follows:ifa==4thena=7end
Example #
#!/usr/bin/ruby#-*- coding: UTF-8 -*-x=1ifx>2puts"x >
2"elsifx<=2andx!=0puts"X is 1 "elseputs" and cannot determine the value of x "end
x is 1
6.16.2. Ruby
if
modifier # Grammar #
codeifcondition
if
modifier phrase indicates that when
if
will not be implemented until the condition on the right is established.
if
, the formula on the left. That is, if
conditional
is true, execute
code
.Example #
#!/usr/bin/ruby$debug=1print"debug\\n"if$debug
debug
6.16.3. Ruby
unless
statement # Grammar #
unlessconditional[then]code[elsecode]end
unless
formula sum
if
has the opposite effect, that is, if
conditional
is false, execute
code
. If
conditional
is true, execute
else
clause specified in the
code
.Example #
#!/usr/bin/ruby#-*- coding: UTF-8 -*-x=1unlessx>2puts"x >
2"elseputs"x < 2"end
x < 2
6.16.4. Ruby
unless
modifier # Grammar #
codeunlessconditional
conditional
is false, execute
code
.Example #
#!/usr/bin/ruby#-*- coding: UTF-8 -*-$var=1print"1 --
This line outputs\\n"if$varprint"2 --
This line does not output\\n"unless$var$var=falseprint"3
-- This line outputs\\n"unless$var
1 -- This line outputs
3 -- This line outputs
6.16.5. Ruby
case
statement # Grammar #
caseexpression[whenexpression[,expression...][then]code]...[elsecode]end
case
to one first
expression
the matching judgment is made, and then the branches are selected according to the matching results.
===
operator comparison
when
specified
expression
.If it is consistent, then execute it
when
part of the content.
then
. If you want to write a complete
when
type, you must use the
then
separate conditional andprogram blocks. As follows:whena==4thena=7end
caseexpr0whenexpr1,expr2stmt1whenexpr3,expr4stmt2elsestmt3end
\_tmp=expr0ifexpr1===\_tmp\|\|expr2===\_tmpstmt1elsifexpr3===\_tmp\|\|expr4===\_tmpstmt2elsestmt3end
Example #
#!/usr/bin/ruby#-*- coding: UTF-8
-*-$age=5case$agewhen0..2puts"baby"when3..
6puts"child"when7..12puts"child"when13..18puts"
youth"elseputs"Other age groups"end
child
case
the expression part of the is omitted, the first
when
expression whose conditional part is true.foo=falsebar=truequu=falsecasewhenfoothenputs'foo is
true'whenbarthenputs'bar is true'whenquuthenputs'quu is true'end#display
"bar is true"