Ruby Ruby Array Indexes Loop Example Ruby Array Loop Example Ruby Do-While Loop Example Ruby Each Loop Example Ruby Each_Index Loop Example Ruby Each_with_Index Loop Example Ruby For Loop Example Ruby Loop Number Array Ruby Loop Range Example Ruby Loop String Array Ruby Matrix Loop Example Ruby Nested Loop Examples Ruby Until Loop Example Ruby Upto Example Ruby While Loop Example Tech

20 Practical Ruby Loop Command Examples – For, Each, While, Until

20 Practical Ruby Loop Command Examples – For, Each, While, Until

Looping via stuff is a vital facet of any programming language.

In a number of of our earlier tutorials, we defined intimately numerous loops together with python for loop, for loops in c programming, loops in awk scripting, loops in bash shell scripting, and so forth.

The main target of this text is on learn how to loop by means of stuff in Ruby.

In Ruby programming language, on a really high-level, the next are eight several types of instructions (features) that you should use to loop by means of stuffs.

  1. Loop
  2. Until
  3. Whereas
  4. Do – Whereas
  5. For
  6. Every
  7. Upto
  8. Occasions

On this tutorial, we’ll talk about the next 20 examples that may cowl all the Ruby loop associated instructions.

  1. Fast Abstract – For the Impatient
  2. Loop Command – Simply Retains Looping
  3. Loop Command – Escape of It
  4. Loop Command – Simply Skip one Specific Loop utilizing Subsequent
  5. Until Command – Maintain looping till it’s true
  6. Whereas Command – Whereas it’s true, maintain looping
  7. Do-Whereas Loop – Whereas on the finish of the block
  8. For Command – Loop by means of a Quantity Vary
  9. For utilizing Array – Loop by means of an Array of Parts
  10. For utilizing Array Size
  11. For utilizing Array Measurement
  12. For inside a For – Nested For Loops
  13. Every Command – Fashionable Loop Technique in Ruby
  14. Every Command – Loop via A number of Strings in an Array
  15. Each_Index Command – Loop via Solely Array Indexes
  16. Each_with_Index Command – Loop via an Array utilizing Index and Worth
  17. Every inside a Every – Nested Every Command
  18. Every Command – Loop by way of Static Quantity Ranges
  19. Occasions Command – Loop by way of X variety of Occasions
  20. Upto Command – Loop by means of Upto X quantity Ranging from Y

1. Fast Abstract – For the Impatient

For the impatient, fast snippets of main loop instructions are proven under. For extra particulars on these instructions, learn via the complete tutorial.

Loop command:

loop do
places “do something”
finish

Until command:


till rely > 5
places “do something”
finish

Whereas command:

whereas rely <= 5
places “do something”
finish

For command – loop 5 occasions

for i in 1..5 do
places “#i”
finish

For command – loop by way of an array:

for i in myarray do
places “#i”
finish

Every command – loop 5 occasions

(1..5).every do |i|
places “#i”
finish

Every command – loop by way of an array

myarray.every do |component|
places “#element”
finish

Every command – loop by means of an array with each index and parts

myarray.each_with_index do |aspect,i|
places “#i. #element”
finish

Occasions command – loop 5 occasions

5.occasions do |i|
places “#i”
finish

Upto command – loop from 2 by means of 5

2.upto(5) do |i|
places “#i”
finish

2. Loop Command – Simply Retains Looping

The primary is the loop command itself. All it does is that it retains happening an infinite loop.

As you see under, the next primary loop command instance, will repeatedly maintain printing the stuff between the “do” and “end” block till you press Ctrl-C

# cat loop1.rb
loop do
places “The Geek Stuf”
places “Press Ctrl-C to Exit!”
finish

Execute the above program to check it. Ensure you press Ctrl-C to exit out of the loop

# ruby loop1.rb
The Geek Stuf
^CPress Ctrl-C to Exit!
loop1.rb:2: Interrupt
from loop1.rb:1:in `loop’
from loop1.rb:1

Additionally, remember that for just about any loop instructions in Ruby, as an alternative of “do” and “end” you can too specify the physique of the loop inside curly brackets as proven under.

The next instance is strictly similar as above, however utilizing braces .

# cat loop2.rb
loop
places “The Geek Stuf”
places “Press Ctrl-C to Exit!”

Warning: Don’t give on a separate line! Should you do, you’ll get an error message. The next isn’t right because the is on the subsequent line to loop. It ought to be on the identical line as loop command.

# cat loop2.rb
loop

places “The Geek Stuf”
places “Press Ctrl-C to Exit!”

The above instance will throw the next error message.

# ruby loop2.rb
loop2.rb:three: syntax error, sudden tSTRING_BEG, anticipating kDO or ” or ‘(‘
places “The Geek Stuf”
^
loop2.rb:5: syntax error, sudden ”, anticipating $finish

three. Loop Command – Escape of It

The earlier loop command instance is just about ineffective because it simply retains looping via.

So, to interrupt out of a loop command, use the break command as proven under.

Once you do a “break”, it can simply cease the execution of the loop and are available out.

# cat loop3.rb
loop do
places “The Geek Stuff”
break
finish

The above instance will loop by way of only one time.

# ruby loop3.rb
The Geek Stuff

Looping via only one time, is just about ineffective additionally. So, within the following loop command instance, it can loop via complete of 5 occasions earlier than breaking out of the loop.

As you’ll be able to think about, to maintain monitor of the variety of occasions to loop, we’re utilizing a variable referred to as rely and increment it by 1 each time. When the worth of the rely is the same as 6, we’ll escape of the loop.

# cat loop4.rb
rely = 1
loop do
places “#count” ” The Geek Stuff”
rely = rely + 1
if rely == 6
break
finish
finish

The next is the output of the above instance, which loops via precisely 5 occasions.

# ruby loop4.rb
1 The Geek Stuff
2 The Geek Stuff
three The Geek Stuff
four The Geek Stuff
5 The Geek Stuff

four. Loop Command – Simply Skip one Specific Loop utilizing Subsequent

Identical to break, inside a loop command, you can too use subsequent.

Subsequent will simply skip the remainder of the instructions within the loop-block for the present execution, however will proceed the loop from the highest once more.

So, within the following instance, utilizing if command, we’re checking if the worth of rely is three. When it’s three, we’re executing “next” command, which can skip the rest of the loop-block and proceed the loop from the highest once more.

Because of this the next instance will skip solely the third execution of the loop.

# cat loop5.rb
rely = 1
loop do
if rely == three
rely = rely + 1
subsequent
finish
places “#count” ” The Geek Stuff”
rely = rely + 1
if rely == 6
break
finish
finish

The next output signifies that the above instance has skipped the third execution of the loop.

# ruby loop5.rb
1 The Geek Stuff
2 The Geek Stuff
four The Geek Stuff
5 The Geek Stuff

5. Until Command – Hold looping till it’s true

Until is one other sort of ruby command to loop by means of stuff.

In till command, the physique will get executed till the situation is true. i.e Hold looping as lengthy the situation is fake. When the situation turns into true, it can cease the loop.

Within the following instance, so long as the worth of the variable rely isn’t higher than 5, it can maintain looping. When the situation turns into true (i.e when rely is bigger than 5), it can cease looping.

# cat until1.rb
rely = 1
till rely > 5
places “#count” ” The Geek Stuff”
rely = rely + 1
finish

As you see from the output under, the above instance has executed the loop 5 occasions utilizing till command.

# ruby until1.rb
1 The Geek Stuff
2 The Geek Stuff
three The Geek Stuff
four The Geek Stuff
5 The Geek Stuff

6. Whereas Command – Whereas it’s true, maintain looping

The second sort of command to loop via stuff is whereas command.

In whereas command, the physique will get executed as lengthy the situation specified within the whereas command is true.

So, the thought right here is that whereas the situation is true, it’s going to hold looping ceaselessly. So, it’s your duty to ensure the situation turns into false sooner or later in time.

Within the following instance, the whereas will maintain looping the physique till the rely is lower than or equal to five. We’re growing the rely contained in the loop-body.

So, the next instance will loop by means of 5 occasions.

# cat while1.rb
rely = 1
whereas rely <= 5
places “#count” ” The Geek Stuff”
rely = rely + 1
finish

As you see from the output under, the above instance has executed the loop 5 occasions utilizing whereas command.

# ruby while1.rb
1 The Geek Stuff
2 The Geek Stuff
three The Geek Stuff
four The Geek Stuff
5 The Geek Stuff

7. Do-Whereas Loop – Whereas on the finish of the block

Within the earlier instance, we had the whereas command initially of the loop.

Sometimes in most programming languages, once you put the whereas command on the finish of the loop, it’s referred to as do-while loop.

Ruby additionally helps the format of do-while as proven within the following instance.

One factor to remember right here is that, a do-while loop will all the time execute the physique of the loop at-least as soon as.

It’s because we aren’t checking for any situation initially of the loop. So, the primary time the loop is all the time executed.

On the finish of the first loop, the situation is checked. So long as the situation is true, it’s going to hold looping.

So, the next instance will execute the loop 1st time, after which so long as the situation is true, it can maintain looping.

# cat do-while1.rb
rely = 1
start
places “#count” ” The Geek Stuff”
rely = rely + 1
finish whereas rely <= 5

As you see from the output under, the above instance has executed the loop 5 occasions utilizing do-while command.

# ruby do-while1.rb
1 The Geek Stuff
2 The Geek Stuff
three The Geek Stuff
four The Geek Stuff
5 The Geek Stuff

eight. For Command – Loop by means of a Quantity Vary

Virtually all programming languages have a for command to loop by means of stuff. Ruby is not any exception.

The next instance exhibits the way to loop via a variety of numeric values utilizing a easy for loop.

The vary is specified within the format of start-value and end-value separated by two durations (with none area in between them)

Within the following instance, the start-value of the vary is 1. The top-value of the vary is 5. rely is the identify of the variable that maintain the present worth of the loop.

# cat for1.rb
for rely in 1..5 do
places “#count” ” The Geek Stuff”
finish

The next output signifies that the above instance executed the loop 5 occasions utilizing for command

# ruby for1.rb
1 The Geek Stuff
2 The Geek Stuff
three The Geek Stuff
four The Geek Stuff
5 The Geek Stuff

9. For utilizing Array – Loop via an Array of Parts

Aside from giving a quantity vary within the for command, it’s also possible to specify an array of parts to loop by means of.

Within the following instance, we have now an array referred to as “myarray”. Within the for command, we’ve simply specified this array identify to loop via.

This can simply loop via all the weather within the array one after the other utilizing the for command.

# cat for2.rb
myarray = [ 101, 201, 301, 401, 501 ]
for i in myarray do
places “#i”
finish

As you see from the next output, it looped by means of all the weather within the array and printed them accordingly.

# ruby for2.rb
101
201
301
401
501

You can even loop by means of an array with string values as proven under.

# cat for2.1.rb
myarray = [ “The”, “Geek”, “Stuff” ]
for i in myarray do
places “#i”
finish

As you see from the next output, the above instance simply looped via myarray which incorporates three string parts and printed them out.

# ruby for2.1.rb
The
Geek
Stuff

10. For utilizing Array Size

In a for loop command, you may as well specify the array-length within the vary because the end-value to loop via an array as proven under.

# cat for3.rb
myarray = [ “The”, “Geek”, “Stuff” ]
maxsize = myarray.size
for i in 1..maxsize
places “#i” ” The Geek Stuff”
finish

Within the above instance, myarray incorporates complete of three parts. So, marray.size will return three. We’re utilizing this worth within the for command vary as 1..maxsize

So, we aren’t actually looping by means of the array. We’re type of looping via the array indexes not directly. There’s a higher technique to loop via an array utilizing indexes, which is defined in one of many following examples.

As you see from the next output, the loop is executed, precisely three occasions, which is the size of the array.

# ruby for3.rb
1 The Geek Stuff
2 The Geek Stuff
three The Geek Stuff

11. For utilizing Array Measurement

The next instance is similar to the earlier instance. However, we’re utilizing array measurement as an alternative of size.

# cat for4.rb
myarray = [ “The”, “Geek”, “Stuff” ]
for i in zero…myarray.measurement
places “#i” ” The Geek Stuff”
finish

Needless to say:
Array size will return what number of parts are within the array. We acquired three in earlier instance. So, if you wish to loop by way of from 1, 2, three — use array size.
Array measurement will return complete variety of parts within the array minus 1. So, we obtained 2 on this instance. So, if you wish to loop via from zero, 1, 2 — use array measurement.

As you see from the next output, the array measurement returned 2. So, we’re looping by means of from zero by means of 2 in our for loop vary.

# ruby for4.rb
zero The Geek Stuff
1 The Geek Stuff
2 The Geek Stuff

12. For inside a For – Nested For Loops

You can even have for loop inside a for loop. That is referred to as nested for loops as proven within the following instance.

The outer for loop will loop by way of the “names” array. The internal for loop will loop by means of the “things” array.

So, for every individual within the names array, it is going to loop by way of each merchandise within the issues array.

# cat for5.rb
names = [ “John”, “Jason”, “Lisa” ]
issues = [ “Chocolate”, “Sugar” ]
counter = 1
for identify in names
places “#counter. #name likes: “
counter = counter + 1
for merchandise in issues
places ” #item”
finish
finish

As you see from the next output, for every individual, it has looped by way of all of the issues utilizing a nested for loop format.

# ruby for5.rb
1. John likes:
Chocolate
Sugar
2. Jason likes:
Chocolate
Sugar
three. Lisa likes:
Chocolate
Sugar

13. Every Command – In style Loop Technique in Ruby

In Ruby, the advisable technique to loop by means of stuff is utilizing every command as proven under.

The syntax of every command is bit totally different than the normal for loop, whereas loop, and so forth.

The next instance explains find out how to loop by means of an array of numbers utilizing every command in ruby.

# cat iterator1.rb
counter = [ 1, 2, 3, 4, 5 ]
counter.every do |i|
places “#i The Geek Stuff”
finish

Within the above:

  • counter is the identify of the array that incorporates array of numbers.
  • counter.every – Right here we’re including .every to the array variable identify. This can loop via the counter array.
  • do … finish – That is the loop physique (identical to different loop command examples)
  • |i| – That is particular to every command. This means that the variable i’ll maintain the present worth of the array in the course of the looping. You need to use this variable to control the person array factor in aspect the array.

As you see from the next output, we’ve looped via the array variable utilizing the every command and printed the output.

# ruby iterator1.rb
1 The Geek Stuff
2 The Geek Stuff
three The Geek Stuff
four The Geek Stuff
5 The Geek Stuff

14. Every Command – Loop by way of A number of Strings in an Array

That is just like the earlier every instance, however as an alternative of looping by means of an array of numbers, right here we’re looping by way of an array of strings.

# cat iterator2.rb
title = [ “The”, “Geek”, “Stuff” ]
counter = 1
title.every do |i|
places “#counter. #i”
counter = counter + 1
finish

As you see from the next output, the every command has looped by way of out title array and printed all the person parts one after the other.

# ruby iterator2.rb
1. The
2. Geek
three. Stuff

15. Each_Index Command – Loop by way of Solely Array Indexes

In the event you simply need to loop via the indexes of the array and never the array parts themselves, then use the each_index command as proven under.

# cat iterator4.rb
title = [ “The”, “Geek”, “Stuff” ]
title.each_index do |i|
places “#i”
finish

Within the above:

  • title is the array that accommodates three string parts.
  • title.each_index – This means that we wish to loop by means of the indexes of our title array (not the array parts, simply the index of the weather)
  • do … finish – That is the loop physique (identical to different loop command examples)
  • |i| – This means that the variable i’ll maintain the present worth of no matter we’re looping via. Since we’re looping via the indexes, this can maintain the present worth of the index that we’re looping by way of.

As you see from the next output, this has printed the indexes of the array parts. Since we’ve three parts within the array, this instance has printed zero, 1 and a couple of.

# ruby iterator4.rb
zero
1
2

Please take into account that array index all the time begins from zero (not from 1)

16. Each_with_Index Command – Loop by means of an Array utilizing Index and Worth

Whereas it’s nice that every command loops by means of parts, and each_index loops by means of indexes.

What if you wish to loop via each indexes and parts.

That’s precisely why we’ve each_with_index command as proven within the following instance.

On this instance, the each_with_index will loop via every particular person merchandise, and retailer each the component worth and index worth in the course of the loop-body execution in two totally different variables.

# cat iterator3.rb
title = [ “The”, “Geek”, “Stuff” ]
title.each_with_index do |component,i|
places “#i. #element”
finish

Within the above:

  • title is the array that incorporates three string parts.
  • title.each_with_index – This means that we wish to loop via each the array values and indexes
  • do … finish – That is the loop physique (identical to different loop command examples)
  • |aspect,i| – Right here we’re specifying two variables. The first variable (component) will maintain the worth of the person array factor. The 2nd variable i’ll maintain the worth of the indexes of that exact component.
  • places “#i. #element” – Within the loop physique, we’re utilizing each particular person component and it’s corresponding index worth.

As you see from the next output, this instance prints each the index and the person array aspect worth.

# ruby iterator3.rb
zero. The
1. Geek
2. Stuff

17. Every inside a Every – Nested Every Command

You can even have every inside an every. That is referred to as Nested every, which is analogous to the nested for loop instance that we noticed earlier.

The outer for loop will loop via the “names” array. The inside for loop will loop by means of the “things” array.

So, for every individual within the names array, it is going to loop by way of each merchandise within the issues array.

# cat iterator5.rb
names = [ “John”, “Jason”, “Lisa” ]
issues = [ “Chocolate”, “Sugar” ]
counter = 1
names.every do |identify|
places “#counter. #name likes: “
counter = counter + 1
issues.every do |merchandise|
places ” #item”
finish
finish

As you see from the next output, for every individual, it has looped by means of all of the issues utilizing a nested every loop format.

# ruby iterator5.rb
1. John likes:
Chocolate
Sugar
2. Jason likes:
Chocolate
Sugar
three. Lisa likes:
Chocolate
Sugar

18. Every Command – Loop by means of Static Quantity Ranges

As an alternative of looping by way of an array, you may as well loop via static numbers from a variety as proven within the following instance.

# cat iterator6.rb
(1..5).every do |counter|
places “#counter The Geek Stuff”
finish

Within the above:

  • (1..5).every – This means that we’ll be looping via the given vary utilizing every command. The given vary is 1 by way of 5
  • do … finish – That is the loop physique (identical to different loop command examples)
  • |counter| – This means that the variable counter will maintain the present worth of no matter we’re looping by means of.

As you see from the next output, ther above command has looped by way of the given static vary utilizing every command.

# ruby iterator6.rb
1 The Geek Stuff
2 The Geek Stuff
three The Geek Stuff
four The Geek Stuff
5 The Geek Stuff

19. Occasions Command – Loop by way of X variety of Occasions

Occasions is a really useful loop command, which can provide help to to shortly loop via x variety of occasions.

The next instance will loop by means of the loop-body 5 occasions.

# cat times1.rb
5.occasions do |i|
places “#i The Geek Stuff”
finish

Within the above:

  • 5.occasions – This means that variety of time that we wish to loop via. Right here we’ll be looping by means of 5 occasions
  • do … finish – That is the loop physique (identical to different loop command examples)
  • |i| – This means that the variable i’ll maintain the present worth of the loop counter i.e 1st loop, or 2nd loop, or third loop, and so on.

One essential factor to remember is that, the occasions command will all the time begin from zero.

So, once we say 5.occasions, it means: zero, 1, 2, three and four

As you see from the next output, the above instance looped via 5 occasions, however ranging from zero by means of four.

# ruby times1.rb
zero The Geek Stuff
1 The Geek Stuff
2 The Geek Stuff
three The Geek Stuff
four The Geek Stuff

20. Upto Command – Loop by means of Upto X quantity Ranging from Y

One other helpful command is upto.

In contrast to occasions command, you’ll be able to specify the beginning quantity for the loop.

The next instance is just like the earlier instance, however it begins from 1 (as an alternative of zero) and loops by means of 5 occasions.

# cat upto1.rb
1.upto(5) do |i|
places “#i The Geek Stuff”
finish

As you see from the next output, the above instance looped by means of 5 occasions, however ranging from 1 by means of 5.

# ruby upto1.rb
1 The Geek Stuff
2 The Geek Stuff
three The Geek Stuff
four The Geek Stuff
5 The Geek Stuff

Within the following instance, we’re ranging from three and looping by means of upto 6. So, it will loop by way of: three, four, 5 and 6

# cat upto2.rb
three.upto(6) do |i|
places “#i The Geek Stuff”
finish

The next instance has looped via four occasions utilizing upto command, however ranging from three by way of 6.

# ruby upto2.rb
three The Geek Stuff
four The Geek Stuff
5 The Geek Stuff
6 The Geek Stuff

Should you loved this text, you may also like..