Ruby Conditional

Ruby provides several common conditional structures. Here, we will explain all the conditional statements and modifiers available in Ruby.

Ruby if...elseStatement

Syntax

if conditional [then] code... [elsif conditional [then] code...]... [else code...] end

ifAn expression is used for conditional execution. The valuefalseandnilfalse is false, all other values are true. Note that Ruby uses elsif, not else if or elif.

Ifconditionalis true, then executecode. Ifconditionalis not true, then execute the one specified in the else clausecode。

Usually we omit the reserved word then. If you want to write a complete if expression on one line, you must separate the condition from 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 is greater than 2" elsif x <= 2 and x!=0 puts "x is 1" else puts "The value of x cannot be determined" end

Try it »

The above example outputs:

x 是 1

Ruby ifModifier

Syntax

code if condition

The if modifier phrase means that the expression on the left of if is executed only when the condition on the right of if is true. That is, ifconditionalis true, then executecode。

Example

#!/usr/bin/ruby $debug=1 print "debug\n" if $debug

Try it »

The above example outputs:

debug

Ruby unlessStatement

Syntax

unless conditional [then] code [else code ] end

The unless expression works opposite to the if expression, that is, ifconditionalis false, then executecode. Ifconditionalis true, then execute the one specified in the else clausecode。

Example

#!/usr/bin/ruby # -*- coding: UTF-8 -*- x=1 unless x>2 puts "x is less than or equal to 2" else puts "x is greater than 2" end

Try it »

The above example outputs:

x 小于或等于 2

Ruby unlessModifier

Syntax

code unless conditional

Ifconditionalis false, then executecode。

Example

#!/usr/bin/ruby # -*- coding: UTF-8 -*- $var = 1 print "1 -- This line outputs\n" if $var print "2 -- This line does not output\n" unless $var $var = false print "3 -- This line outputs\n" unless $var

Try it »

The above example outputs:

1 -- 这一行输出
3 -- 这一行输出

Ruby caseStatement

Syntax

case expression [when expression [, expression ...] [then] code ]... [else code ] end

case first performs aexpressionmatching judgment, and then selects a branch based on the match result.

It uses the===operator to comparewhenthe specifiedexpression, and if they match, then executewhenthe content of that part.

Usually we omit the reserved word then. If you want to write a complete when expression on one line, you must separate the condition from the program block with then. As shown below:

when a == 4 then a = 7 end

Therefore:

case expr0 when expr1, expr2 stmt1 when expr3, expr4 stmt2 else stmt3 end

Basically similar to:

_tmp = expr0 if expr1 === _tmp || expr2 === _tmp stmt1 elsif expr3 === _tmp || expr4 === _tmp stmt2 else stmt3 end

Example

#!/usr/bin/ruby # -*- coding: UTF-8 -*- $age = 5 case $age when 0 .. 2 puts "Baby" when 3 .. 6 puts "Child" when 7 .. 12 puts "child" when 13 .. 18 puts "Juvenile" else puts "Other age" end

Try it »

The above example outputs:

child

When the "expression" part of case is omitted, the first when condition part that is true is evaluated.

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 #Displays 'bar is true'
Other extensions