Ruby - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Ruby Flow Control

Ruby Flow Control

shape Introduction

This chapter demonstrates about the Ruby Flow Control which is used to make the right decisions, user can make the right decision by using the flow control and following are the concepts covered in this chapter.
  • Combining expressions
  • Comparisons

Combining expressions

shape Description

In Ruby Flow Control User can combine the multiple conditional expressions together to create specific scenario which can be done by using the && and || operators. "and" && operator in which expression both the left and right operators are true for the entire expression to be evaluated true the code below demonstrates the and operator as shown below. [ruby] irb :001 > (4 == 4) && (5 == 5) => true irb :002 > (4 == 5) && (5 == 5) => false irb :002 > (4 == 5) && (5 == 6) => false [/ruby] || - the "or" operator. Either the expression to one side must be valid, or the expression to the right must be valid for the whole expression to be assessed to genuine. The code below demonstrates the or operator as shown below. [ruby] irb :001 > (4 == 4) || (5 == 5) => true irb :002 > (4 == 5) || (5 == 5) => true irb :002 > (4 == 5) || (5 == 6) => false [/ruby] ! - the "not" operator. When user using before a boolean expression then it will change the boolean value to its inverse the code below demonstrates the not operator as shown. [ruby] irb :001 > !(4 == 4) => false [/ruby] In Ruby Flow Control Ruby follows order of precedence when evaluating the multiple expressions. The list below demonstrates the order from highest precedence to lowest precedence as shown below.
<=, <, >, >= Comparison
==, != Equality
&& Logical AND
|| Logical OR

Comparisons

shape Description

In Ruby Flow Control Comparisons are always return the boolean value which may contain either true or false. < - The "less than" symbol. Anything to left side of the symbol has a lower value than anything to right side of the symbol. > - The "greater than" symbol. Anything to right side of the symbol has a higher value than anything to left side of the symbol. The code below demonstrates the less then and greater then symbols as shown below. [ruby] irb :001 > 4 < 5 => true irb :002 > 4 > 5 => false [/ruby] <= - The "less than or equal" symbol. Anything to left side of the symbol has a lower or equal value than anything to right side of the symbol. >= - The "greater than are equal" symbol. Anything to right side of the symbol has a higher or equal value than anything to left side of the symbol. [ruby] irb :001 > 4 <= 5 => true irb :002 > 5 >= 5 => true irb :003 > 4 >= 5 => false irb :004 > 4 >= 3 => true irb :005 > 4 >= 4 => true [/ruby] == - The "is equal to" operator. Anything to left side of the symbol is precisely equivalent to anything on the right. The code below demonstrates the equal operator as shown below. [ruby] irb :001 > 5 == 5 => true irb :002 > 5 == 6 => false irb :003 > '5' == 5 => false [/ruby]

Summary

shape Key Points

  • Comparison compare both sides operators.
  • Ruby follows order of precedence.
  • Comparisons always returns Boolean value.