Table of contents
1.
Introduction
2.
Altering Control Flow
3.
The Retry statement
4.
Examples for Retry Statement
4.1.
Example 1
4.2.
Example 2
4.3.
Example 3
5.
Frequently Asked Questions
5.1.
What is retry: altering the control flow in Ruby phenomenon?
5.2.
What are loops in Ruby?
5.3.
What is retry in Ruby?
5.4.
What is the difference between redo and retry statements?
5.5.
What are the different types of retry statements in Ruby?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Retry: Altering the Control Flow in Ruby

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Retry: altering the control flow in Ruby is primarily utilized in an exception handling environment. Control shifts to the rescue block, where the exception is dealt with when a program encounters an exception inside a begin block. The execution of the code below the rescue block often comes next. Hence, the begin block's code only has one opportunity to carry out its task.

Coding Ninja Ruby

In the following article, we will deal with retry: altering the control flow in the Ruby phenomenon and discussing it in detail.

Altering Control Flow

Ruby offers several statements that change the flow of control in a Ruby program in addition to conditionals, loops, and iterators. These statements include

  • Return
    It causes a method to finish and give its caller a value.
    Check more on: Return keyword in Ruby
     
  • Break
    It triggers an iterator or loop to end.
    Check more on: Break keyword in Ruby
     
  • Next
    It causes an iterator (or loop) to skip the remaining iterations in favor of the next.
    Check more on: Next keyword in Ruby
     
  • Redo
    Begins an iterator or loop over from the beginning.
    Check more on: Redo keyword in Ruby
     
  • Retry
    Re-evaluates an expression's entirety while restarting an iterator. 
    Let us dive into the ‘Retry’ keyword of Ruby in detail.

The Retry statement

Retry statements are used to resume an iterator based on any method call from the beginning of a specific condition. To put it simply, the retry statement starts a new process with control. Retry statements are typically not used frequently. 

Some functions merit another go. There are many situations where one could desire for the beginning block to make amends. For instance, as a busy server takes longer to respond, HTTP requests and database calls occasionally fail or time out on the first try. Before giving up in such circumstances, we might want to rerun the block a few more times.

Only Ruby version 1.8 will support it.

Since the retry statement is regarded as a deprecated language feature, it has been eliminated from Ruby versions 1.9 and beyond. Most online IDEs utilize version 1.8 or higher, it may never run on them.

Examples for Retry Statement

Example 1

The following Ruby program demonstrates the use of the retry statement.

#!/usr/bin/ruby
begin
   for x in 1..10
       raise if x > 3
       puts "Value of the local variable is #{x}."
   end
rescue
   retry
end
You can also try this code with Online Ruby Compiler
Run Code


Output:

Output for retry in ruby

The output goes on in an infinite loop.

Example 2

The following example again illustrates the use of the retry statement.

#!/usr/bin/ruby
begin
   attemptAgain ||= 0
   puts "Attempt again to execute the code block #{ attemptAgain }"
   raise "We are raising this exception each time to check the count of attemptAgain"
rescue
   puts "Ruby 'retry' statement is called"
   retry if (attemptAgain += 1) < 5
end
You can also try this code with Online Ruby Compiler
Run Code


Output: 

Output for retry in ruby

Example 3

#!/usr/bin/ruby
# Use of the 'do' loop
4.times do |x|
  begin
      puts "Iteration No. #{x}"
      raise if x > 2
          rescue
          # Use of the 'retry' statement
          puts "Here the retry statement is called as it will be falling under rescue block #{x}"
          # retry
      end
  end
You can also try this code with Online Ruby Compiler
Run Code


Output:

Output for retry in ruby

If we undo the comment from line no. 10 and use the ‘retry’ statement, then the output will be like this:

Iteration No. 0
Iteration No. 1
Iteration No. 2
Iteration No. 3
Here the retry statement is called as it will be falling under rescue block 3
Iteration No. 3
Here the retry statement is called as it will be falling under rescue block 3
Iteration No. 3
Here the retry statement is called as it will be falling under rescue block 3
Iteration No. 3
.
.
.


The output goes on in an infinite loop.

Note: The statement retry: altering the control flow in Ruby only works within the begin/rescue block.

Frequently Asked Questions

What is retry: altering the control flow in Ruby phenomenon?

A control flow construct is a language feature that alters the natural flow of the source code by conditionally or unconditionally branching to a different statement or location. Programming and web development both depend on control flow.

What are loops in Ruby?

Programming languages provide looping feature that makes it possible to continually execute a set of instructions or functions depending on whether any of the conditions are true or false. Ruby has various loop types for handling condition-based scenarios in programs, making the programmer's job easier. The Ruby loops are while loop, for loop, do..while loop, and until loop.

What is retry in Ruby?

retry is mainly utilized in Ruby's exception handling environment. Control shifts to the rescue block, where the exception is handled when your program runs into an error inside a begin block. The execution of the code under the rescue block often comes next.

What is the difference between redo and retry statements?

To repeat a loop section, we can either redo or retry. Redo merely repeats the current iteration, whereas retry repeats the entire loop. However, they differ in how much they re-execute.

What are the different types of retry statements in Ruby?

The first type is a plain retry statement, which repeatedly runs a code block until it succeeds. A retry-with-exception statement, which will execute the code block once more but throws an exception if it fails, is the second type.

Conclusion

In this article, we have extensively discussed the retry: altering the control flow in Ruby phenomenon. We began with a brief introduction to Ruby, followed by a list of all the statements that alter the control flow, and looked at retry: altering the control flow in Ruby in detail. 

After reading about the retry: altering the control flow in Ruby, are you not feeling excited to read/explore more articles on the Ruby programming language? Don't worry; Coding Ninjas has you covered. To learn, see RubyRuby coding NinjasDocumentationOfficial Ruby FAQ, and Ruby Koans.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our courses to give your career an edge over others!

Thank you

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass