Ruby is an open source programming language which has been gaining in popularity lately due to its simplicity and the focus and emphasis it lays on productivity. This programming language uses a very user-friendly syntax which is easy to read and write and hence provides ease of usability. The loops in ruby are the programming tools which help the programmer in executing the same block of code repeatedly a certain number of times. This saves time and effort as the same code need not be written again and again and the programming becomes more efficient. There are a number of loops in Ruby and we are going to discuss each of these here in detail. All the different loops have their specific usage and certain properties related to their applications separate them from each other.
Before we move further and start discussing about the different types of loops in Ruby, it would be a good idea to have some basic understanding of programming in Ruby. Here is a great beginner’s course about programming in Ruby which will give you a good introduction about the concepts and will nicely prepare you for going through the concepts which are going to be discussed here. This course has been designed specifically for the beginners in programming in Ruby but can also be used by the expert programmers to an equally good effect to simply brush up their basics.
To read more about the concepts of programming in Ruby you can check out this piece about learning programming in Ruby. This well-written article discusses all the important programming techniques in greater detail and provides an in-depth account of the basic tools and instruction sets.
Now we are going to discuss all the different loops in Ruby one by one.
The while loop in ruby executes the code while the condition which has been specified is true. As soon as the condition becomes untrue the loop breaks. The syntax which is used for executing this loop is:
while condition [do]
In this syntax, the ‘code’ is the code which needs to be executed and the ‘condition’ specifies the condition which needs to be true for the code to be executed. The code will keep on being executed till the condition holds true and the loop will terminate the moment the condition ceases to be true.
In while loop, the exact number of times for which the code needs to be executed is not directly specified. It can be specified by using some appropriate condition or it can be run repeatedly until the specified condition holds. In such a scenario, the programmer also does not know the exact number of times the code will be executed. In such cases when the code stops being executed, it signifies the completion of a particular event or fulfillment of a particular condition.
The for loop, unlike the while loop, is used to execute the code a fixed number of times. In the for loop, the code is executed once for each element in the expression. The syntax used is:
for variable in expression [do]
Here in this syntax, the ‘expression’ specifies a particular range. The code specified by ‘code’ will be executed once for each value in the range specified by ‘expression’. By using the for loop it is possible to control the number of times the code will be executed.
In a for loop, the number of times the code will be executed can be controlled to a greater degree by the programmer. As the code is executed once for each value in the expression, by specifying the expression appropriately, the code can be executed for a specific number of times. Hence a for loop provides greater control.
The until loop is the exact opposite of the while loop. In this loop the execution occurs till the condition which has been specified is false. As soon as the condition becomes true, the loop breaks. The syntax to be used here is,
until condition [do]
In this syntax, ‘condition’ specifies the condition which needs to be false for the code specified by ‘code’ to be executed. As soon as the condition becomes true the loop will terminate.
In the until loop also, just like the while loop, the number of times the code will be executed cannot be controlled directly. Though the advantage of using an until loop is that in an until loop the execution of the code will stop as soon as the specified condition becomes true and hence it can be used as some kind of an alarm for knowing exactly when the specified condition becomes true. This the main advantage of using an until loop over the while loop.
Here is a great course about programming in Ruby which will teach you about all the loops in an exhaustive manner and will enable you to write the basic programs using these loops and other basic programming tools.
The break statement in ruby is used to terminate a loop. It terminates the most internal loop in the program as per wherever the break statement is used. The syntax used is quite simple:
The next statement jumps to the next iteration of the most internal loop in a particular program. This statement is used to terminate the execution of a particular block of code and to initiate the execution of the next block using only one single instruction. The syntax used is:
The redo statement restarts the execution of the code which is being executed. This instruction is mostly used in conjunction with other loop instructions. The syntax used is:
Basically the next, break and redo statements all do the same task. They terminate a loop. But the basic difference in break, next and redo statements is that the break statement simply terminates the loop, the next statement terminates and then jumps to the next instruction and the redo statement restarts the execution of the instruction.
These are the loops which are available in Ruby. The loops provide great flexibility and help in writing programs more efficiently. When loops are used along with other programming tools like arrays, the programs which can be created are amazingly complex in functionality yet very easy to read and write. This is the most important feature about Ruby which has lead to a significant rise in its popularity in recent times.
All the programming techniques which have been discussed here are not self sufficient and a good understanding of the other basic concepts as well will enable you to use these tools in the best possible way. Here is a great course about the basic concepts of programming in Ruby which will teach you about all the loops and the associated concepts and will enable you to write the basic programs in Ruby.
If you want to go a step further and want to learn ruby at an advanced level then here is another course which covers the concepts of programming in Ruby right from the basics to all the concepts at an advanced level. This course will help you become a master in programming in Ruby. So go ahead and start learning!