home PYTHONJAVA
 

Ruby conditional

Ruby provides several very common conditional structures. Here we will explain all the conditional statements and the 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 instead of else if and elif.

If conditional is true, execute code. 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 a complete if expression on a line, you must separate the conditional and program blocks with then. As shown below:

if a == 4 then a = 7 end

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- x=1 if x > 2 puts "x more than the 2" elsif x <= 2 and x!=0 puts "x Yes 1" else puts "Unable to know the value of x " end

The output of the above example:

x is 1

Ruby if modifier

Syntax

code if condition
The

if modifier indicates that the expression to the left of if is executed when the condition to the right of if is true. That is, if conditional is true, execute code.

Instance

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

The output of the above example:

Debug

Ruby unless Statement

grammar

unless conditional [then] code [else code ] end

unless and if have the opposite effect, ie if conditional is false, then code is executed. If conditional is true, the code specified in the else clause is executed.

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- x=1 unless x>2 puts "x Less than 2" else puts "x Less than 2" end

The output of the above example is:

x is less than 2

Ruby unless modifier

Syntax

code unless conditional

in case conditional False, then execute code

Instance

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

The output of the above example:

1 -- this line of output
3 -- this line of output

Ruby case Statement

Syntax

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

case first matches a expression and then performs branch selection based on the matching result.

It uses the === operator to compare expression specified by when, and if it is consistent, execute when Part of the content.

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

when a == 4 then a = 7 end

So:

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
#!/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 ages" end

The output of the above example is:

Child

When the "expression" part of the case is omitted, the expression that the first when condition part 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 # "bar is true"





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.