Complete Tour Guide of Programming Language Ruby – Episode 3

Previously:

Okay, so people, you have made it to the third episode of the Ruby programming guide. By now, you have gotten to know the basic information about the Ruby and its different types of operators and variables. So, in this third episode, we are going to study numerous individual features of Ruby, like looping, hash, etc. So, let’s begin the final journey.

Looping in Ruby

Looping is used in Ruby to execute the same code blocks for a number of times. Out of all the Ruby loops, while loop is used most in the programming language. It helps in executing the code block in the condition when the given situation is true. The fruitful example of the looping is:

$j = 0

 

$num = 10

 

while $j < $num do

 

puts “Inside loop j =

 

$j +=1

 

End

Other Types of Looping

The until the statement is the opposite to the while looping. It executes the block of code when the condition is false. The until statement loop works like this:

$j = 0

 

$num = 5

 

until $j > $num do

 

puts “Inside the loop j =

 

$j +=1;

 

End

The for loop used in the Ruby programming language for the execution of code, once for each of the elements in an expression. The example of for loop will be:

for an in 0..5

 

puts “Value of variable is

 

end

The break statement is the next type of loop which helps in the termination of all internal loop. The example to understand break statement is here:

for a in 0..5

 

if a > 2 then

 

break

 

end

 

puts “Value of the variable is

 

end

 

Output

 

Value of the variable is 0

 

Value of the variable is 1

 

Value of the variable is 2

The next statement is the loop which helps in jumping to the next iteration of the most internal loop. To go inside the most internal loop this next statement loop is very useful. The next statement loop works in the actual environment as per:

for a in 0..5

 

if a < 2 then

 

next

 

end

 

puts “Value is

 

end

 

Output

 

Value is 2

 

Value is 3

 

Value is 4

 

Value is 5

So, after jumping from one iteration of an internal loop to another, if you want to restart the iteration of the most internal loop, then you need the redo statement. The redo statement helps to restart yield or call if called within the block. This all done by the redo statement without checking the loop conditions. That’s why this loop is referred to as the infinite loop also. The feverish example of the redo statement loop is;

for a in 0..5

 

if a < 2 then

 

puts “Value is

 

redo

 

end

 

end

 

Output

 

Value is 0

 

Value is 0

 

…………

Well, guys, I have initially planned to end the Ruby programming guide after the third series, but I found so much interesting information on the features of Ruby that I can not force myself to wrap series yet. So, you, guys have to share little more love and appreciation with us on the last and final series of the Ruby. In the next series, we will study the hash, array, and blocks with the examples. So, till then keep reading and enjoying our blogs.

Send a Message