Menu Close

Articles

Ruby: Decision

Decision statement is one of the most important in any programming language. It is used to make a logical deduction of a conclusion based on several known premises. Once a decision is made you can control the execution of next statements.

Conditional

The “conditional” is a “logical expression” that you use to make a decision. Conditionals are used to enable execution of one or more statements when a condition is true or disable execution when false.

The conditional can be created using two alternate keywords: “if” or “unless”. Alternative keyword “unless” is equivalent with “if” and negative condition. You can negate a condition using the complementary operators or negation: For example: if !(a > b) is equivalent to: unless (a >= b)

Conditional execution:

In Ruby you can apply conditional to any statement as a suffix. This may look a bit strange at beginning for developers of Python or C. Let’s investigate an example and you will grasp this idea quickly:

Homework: Open this example live: ruby decision

  • run it once, enter: 0
  • run it again, enter: 5
  • run it again, enter:  10

Conditional expression:

You can use “if” keyword to produce one value depending on a specific condition. This technique is an alternative to “ternary” operator “?”. It may be redundant but it is more readable and fun to use:

Note: As you can see in above example “if” keyword is used in two ways. First time it is used as expression, second time is used as statement. Also, keyword “then” is optional.

Branches

Imagine a river, it can split in branches and then it can merge back again into main stream. This is how you should picture your program. Like a river that flow downstream. This is called “logical flow”. We can control creation of branches using conditionals.

Example:

Homework: Open this example live and run 3 times: ruby branches

  1. run and enter a digit: 5
  2. run and enter a lowercase letter: x
  3. run and enter one uppercase letter: L

Ladder

You can create multiple branch selection statement using “elsif” keyword. In other languages this keyword is “elif” or “else if”. We use ladder to analyze multiple conditions and avoid using nested if statements.

Example

Homework: Open this example and run 4 times: ruby ladder

  1. enter a digit: 4
  2. enter a uppercase character: G
  3. enter a lowercase character: p
  4. try to make an error: enter ?46

Have fun already? If you have questions, contact us on Discord!

Selection

Remember in Ruby you can do one thing in many ways? You can replace “ladder” with “selection” statement. In other languages this statement is called “switch” but Ruby is using “case” instead.

This statement is called “selection” because it enables execution of one specific branch depending on a condition or value. It is more flexible than “switch” and in my opinion more useful.

Example:

Conditional selector:

Value selector:

Both examples are available live, but you can skip this assignment :mrgreen:

Switch simulation:

Homework: run this example live 2 times: ruby switch

  1. first time enter number: 4
  2. second time enter number: 2

Note: There is no break required like in other languages. Also there is no fall through. Do your best to use a combination of selectors and conditionals to resolve special cases like in example the “2” case.

Read next: repetition