Basic Concepts Of Ruby Language - Part Two

In my last article, we learned some topics like strings, operators etc. in the Ruby language. Likewise, today we are going to learn about some important concepts in Control structures of Ruby. You have already learned this concept in other languages but here it has important expressions and conditions. You can read the first part here -

Ok, let us see the content that we are going to learn.

CONTENTS

  • Unless Expression.
  • Case Statements.
  • Until Loops.
  • Break Statement.
  • Next Statement.
  • Loop do Statement.

Unless Expression

The Unless expression is the opposite of an If expression. It executes the code when a condition is false.

Basic Concepts Of Ruby Language 

In the above code, you can see the usage of the Unless keyword a < 10. The condition is true but Unless makes it false and executes the Else statement.

You can use an else block with Unless, just like you did with the If expression. The end keyword is also required to close the block. The If and Unless modifiers can also be used to execute the code.

Basic Concepts Of Ruby Language 

Case Statement

  • It is a switch statement.
  • We can check multiple conditions using the if/elsif/else statements.
  • A more simplified and flexible option is case expression, which tests a value in When statement.

Basic Concepts Of Ruby Language 

Multiple values can be tested within a single When by separating the values with commas.
 
Basic Concepts Of Ruby Language 

In this above code, more than one values, i.e., multiple values also can be used if one condition becomes true. This means it displays the output.

An else statement can be provided to execute the code if no When condition matches.

Basic Concepts Of Ruby Language

 

Until Loops

  • The Until loops is the opposite of While loop.
  • It will run while its condition is false.
  • It is like Unless.
  • The below code will let you understand it well.

Basic Concepts Of Ruby Language 

Ranges
  • Range represents a sequence.
  • 0 to 10, 56 to 82 and a to z are all examples of ranges.
  • Ruby has special operators to create ranges with ease.
  • These are the “..” and “…” range operator.
  • The two-dot form creates an inclusive range, while the three-dot form creates a range that excludes the specified high value.
  • Ranges are also useful in for loop.
  • The to_a method is used to convert a range to an array, so we can output it.

Basic Concepts Of Ruby Language

 

Basic Concepts Of Ruby Language 
 
The first condition of a is (1..7) two dots, it displays the all the numbers between 1 and 7 as output. The second condition of b is (79…82) three dots, it displays the numbers of 79 to 81 except the last number as output. The third condition of c is also like the first condition. 

Ranges can also be used in case statements for when values.

Basic Concepts Of Ruby Language 

Break Statement

The break statement can be used to stop a loop. For example, we can try this in a For loop.

Basic Concepts Of Ruby Language 

In this example, the Ranges concept is also included.

Next Statement

The Next statement can be used to skip one iteration of the loop and continue with the next one.

Basic Concepts Of Ruby Language 

This will output only the odd numbers from 0 to 10 because the even number will skip the loop iteration.

Ruby also has the redo statement, which causes the current loop iteration to repeat.

Loop do

  • Another looping statement in Ruby is the loop do.
  • It allows the code to execute until a break condition is achieved.

    Basic Concepts Of Ruby Language
  • This will print the numbers from 0 to 10.
  • When x > 10 evaluates to true, the loop will stop.
  • If we do not include a break condition the loop will run forever.

Summary

Today, we saw some interesting concepts. I hope all of them are understandable. If you have any queries, please ask anything about it.


Similar Articles