Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Ruby Conditional Statements

Ruby Decision Making

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 value false and nil is false, and all other values are true. Note that Ruby uses elsif, not else if or elif.

If both the conditional is true, then execute code. If conditional is not true, then execute the one specified in the else clause code。

Usually we omit the keyword then. If you want to write a complete if expression on a single line, you must separate the condition and the code 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 example above produces the following output:


x 是 1

Ruby if Modifiers

Syntax

code

if

condition

The if modifier means that the expression on the left of if is executed only when the condition on the right of if is true. That is, if conditional is true, then execute code。

Example

!/usr/bin/ruby

$debug

=

1

print

"

debug

\n

"

if

$debug

Try it »

The example above produces the following output:


debug

Ruby unless Statement

Syntax

unless

conditional

[

then

]

code

[

else

code

]

end

The unless form works opposite to the if form, that is, if conditional is false, then execute code. If conditional is true, then execute the one specified in the else clause code。

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 output of the above example is:


x 小于或等于 2

Ruby unless Modifiers

Syntax

code

unless

conditional

If both the conditional is false, then execute code。

Example

!/usr/bin/ruby

-- coding: UTF-8 --

$var

=

1

print

"

1 -- this line is output

\n

"

if

$var

print

"

2 -- this line is not output

\n

"

unless

$var

$var

=

false

print

"

3 -- this line is output

\n

"

unless

$var

Try it »

The example above produces the following output:


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

Ruby case Statement

Syntax

case

expression

[

when

expression

[

,

expression

...

]

[

then

]

code

]

...

[

else

code

]

end

case first performs a match test on an expression expression, and then selects a branch based on the match result.

It uses the **===** operator to compare when specified expression, and if they match, it executes when the contents of that part.

Usually we omit the keyword then. If you want to write a complete when expression on a single line, you must separate the condition and the code 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

"

Infant

"

when

3

..

6

puts

"

Child

"

when

7

..

12

puts

"

child

"

when

13

..

18

puts

"

Teenager

"

else

puts

"

Other age groups

"

end

Try it »

The output of the above example is:


小孩

When the "expression" part of case is omitted, the first when clause whose condition 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

Display "bar is true"