Table of contents
1.
Introduction
2.
Altering Control Flow
3.
The redo statement
4.
Examples
5.
Frequently Asked Questions
5.1.
What is a control flow construct in Ruby?
5.2.
What are loops in Ruby?
5.3.
Explain Redo statements in Ruby?
5.4.
What is the difference between Redo and Retry statements?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Redo: altering the control flow in Ruby

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

Introduction

Redo: altering the control flow in the Ruby phenomenon deals with changing the normal flow of the execution of the statements in a code. Ruby redo statement is used within a loop and performs the repetition of the current iteration of the loop. The redo statement is independent of the loop’s condition and executes after its own condition is fulfilled.

Ruby

In the following article, we will deal with Redo: altering the control flow in Ruby phenomenon in detail and see some code examples regarding the Redo: altering the control flow in Ruby process.

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.
  • break-It triggers an iterator or loop to end.
  • next-It causes an iterator (or loop) to skip the remaining iterations in favor of the next.
  • redo-Begins an iterator or loop over from the beginning.
  • retry-Re-evaluates an expression's entirety while restarting an iterator. 

The redo statement

Redo: altering the control flow in the Ruby phenomenon is required when we need to restart the current iteration of a loop. This is done with the help of the redo statement. The redo and next statement are distinct from one another. Redo statements return control to the top of the block or loop so that iteration may begin again, whereas next statements always move control to the conclusion of the loop where the statement that follows the loop can begin to run.

It also aids in comparing the control flow operators redo and other control flow operators like break and continue. While continue ends the current iteration and advances to the next one, the break ends the loop entirely. While redo simply repeats the current loop.

Examples

The following Ruby programs demonstrate the working of the redo statements in Ruby:

Code1: 

This code repeats the iteration when the variable defined as val reaches the value of four.

# defining a variable
val = 0
  
# using while loop which should give
# output as 0,1,2,3 but here it will 
# output as 0,1,2,3,4
while(val < 4) 
  
# Control returns here when 
# redo will execute
puts val
val += 1
  
# using redo statement
redo if val == 4
  
# ending of while loop
end
You can also try this code with Online Ruby Compiler
Run Code

 

Output:

0
1
2
3
4

 

Code2:

This code repeats the iteration when the variable defined as counter is less than the value of four.

fruits = ["banana", "apple ", "mango", "kiwi"]
cart = []
counter = 0 # for tracking number of redos
for fruit in fruits
    puts "Added #{fruit}  to cart: #{cart.append(fruit)}"
    if fruit == 'apple' # arbitrary condition check
        redo if (counter += 1) < 4 # ⤴ redo three more times
    end
end 
You can also try this code with Online Ruby Compiler
Run Code

 

Output:

Added banana to cart: ["banana"]
Added apple to cart: ["banana", "apple"]
Added apple to cart: ["banana", "apple", "apple"]
Added apple to cart: ["banana", "apple", "apple", "apple"]
Added apple to cart: ["banana", "apple", "apple", "apple", "apple"]
Added mango to cart: ["banana", "apple", "apple", "apple", "apple", "mango"]
Added kiwi to cart: ["banana", "apple", "apple", "apple", "apple", "mango", "kiwi"]

Frequently Asked Questions

What is a control flow construct in Ruby?

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.

Explain Redo statements in Ruby?

To repeat the current loop iteration in Ruby, use the redo statement. redo is constantly used inside of a loop. Without re-evaluating the condition, the redo statement starts the loop again.

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.

Conclusion

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

After reading about the Redo: 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 paid courses to give your career an edge over others!

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

Happy Learning!

Thank you image
Live masterclass