The Wayback Machine - https://web.archive.org/web/20240605183117/https://www.geeksforgeeks.org/ruby-redo-and-retry-statement/
Open In App

Ruby redo and retry Statement

Last Updated : 17 Sep, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

In Ruby, Redo statement is used to repeat the current iteration of the loop. redo always used inside the loop. The redo statement restarts the loop without evaluating the condition again.




# Ruby program of using redo statement
  
#!/usr/bin/ruby
restart = false
  
# Using for loop
for x in 2..20
    if x == 15
        if restart == false
  
            # Printing values
            puts "Re-doing when x = " + x.to_s
            restart = true
  
            # Using Redo Statement
            redo
        end
    end
    puts x
end


Output:

2
3
4
5
6
7
8
9
10
11
12
13
14
Re-doing when x = 15
15
16
17
18
19
20

In above example, redo statement apply when x = 15.

retry Statement:

To repeat the whole loop iteration from the start the retry statement is used. retry always used inside the loop.

Example #1:




# Ruby program of retry statement
  
# Using do loop
10.times do |i|
  begin
    puts "Iteration #{i}"
    raise if i > 2
  rescue
  
    # Using retry
    retry
  end
end


Output:

Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 3
Iteration 3
...

Example #2:




# Ruby program of retry statement
def geeks
  attempt_again = true
  p 'checking'
  begin
  
    # This is the point where the control flow jumps
    p 'crash'
    raise 'foo'
      rescue Exception => e
        if attempt_again
          attempt_again = false
  
      # Using retry
      retry       
    end
  end
end




Previous Article
Next Article

Similar Reads

Ruby | unless Statement and unless Modifier
Ruby provides a special statement which is referred as unless statement. This statement is executed when the given condition is false. It is opposite of if statement. In if statement, the block executes once the given condition is true, however in unless statement, the block of code executes once the given condition is false. Unless statement is us
2 min read
Ruby Break and Next Statement
In Ruby, we use a break statement to break the execution of the loop in the program. It is mostly used in while loop, where value is printed till the condition, is true, then break statement terminates the loop. Syntax : Break Example : # Ruby program to use break statement #!/usr/bin/ruby -w i = 1 # Using While Loop while true # Printing Values pu
2 min read
Ruby | Case Statement
The case statement is a multiway branch statement just like a switch statement in other languages. It provides an easy way to forward execution to different parts of code based on the value of the expression. There are 3 important keywords which are used in the case statement: case: It is similar to the switch keyword in another programming languag
3 min read
Difference between Ruby and Ruby on Rails
1. Ruby : Ruby is an object-oriented scripting language launched in 1995 and is known as a general-purpose programming language. It was programmed in C programming language. Ruby is a secured programming language and its syntax is similar to Perl and Python. It was developed on the principle of user interface design and it is mainly used to develop
3 min read
Statement, Indentation and Comment in Python
Here, we will discuss Statements in Python, Indentation in Python, and Comments in Python. We will also discuss different rules and examples for Python Statement, Python Indentation, Python Comment, and the Difference Between 'Docstrings' and 'Multi-line Comments. What is Statement in Python A Python statement is an instruction that the Python inte
7 min read
How to write an empty function in Python - pass statement?
In C/C++ and Java, we can write empty function as following // An empty function in C/C++/Java void fun() { } In Python, if we write something like following in Python, it would produce compiler error. # Incorrect empty function in Python def fun(): Output : IndentationError: expected an indented block In Python, to write empty functions, we use pa
1 min read
Using Else Conditional Statement With For loop in Python
Using else conditional statement with for loop in python In most of the programming languages (C/C++, Java, etc), the use of else statement has been restricted with the if conditional statements. But Python also allows us to use the else condition with for loops. The else block just after for/while is executed only when the loop is NOT terminated b
2 min read
with statement in Python
In Python, with statement is used in exception handling to make the code cleaner and much more readable. It simplifies the management of common resources like file streams. Observe the following code example on how the use of with statement makes code cleaner. C/C++ Code # file handling # 1) without using with statement file = open('file_path', 'w'
4 min read
Python return statement
A return statement is used to end the execution of the function call and "returns" the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned. A return statement is overall use
4 min read
Python Continue Statement
Python Continue Statement skips the execution of the program block after the continue statement and forces the control to start the next iteration. Python Continue StatementPython Continue statement is a loop control statement that forces to execute the next iteration of the loop while skipping the rest of the code inside the loop for the current i
4 min read
Article Tags :
Practice Tags :