Updated April 4, 2023
Introduction to Ruby retry
In Ruby, retry statement allow us to repeat complete loop from starting, this new for programming tricks, mostly retry statement will be used in case if we are working on any transaction or any other type of looping where every iteration of a loop should be successful or in some specific sequences, in case if we found any issue with any iteration of the running loop we can use the retry statement to start the loop from the beginning, retry statement is always used inside the loop which means there should be any iteration for implementation of the retry statement.
Syntax
In the below we have written a simple syntax for retry statement, we can explain the below syntax with the following steps.
- We are running a beginning loop on the numeric value. Here numeric values can be anything, from 0 to any number.
- Inside the beginning loop, we can perform some operations.
- We have written a condition block which will check for any particular condition according to our requirement.
- In case of the condition if true or success then the exception will be raised and the rescue block will play its job.
- Inside the rescue block, we are checking for we have called the retry statement, which will start the loop from that point only.
- We need to take care a lot while using retry as if it will not be used with proper logic and understanding the loop can go for infinite as it will keep calling retry.
- Here retry should call with a proper limit, which means how many times it should do retry.
See the below syntax for understanding:
numericValue.times do |i|
begin
#Perform some operation the coming data on each iteration
raise conditional expression #If conditional expression successful simply it will fall into rescue block
rescue
# Here we will catch the exception raise inside begin block
retry
end
end
How does the retry Statement work in Ruby?
You must have visited eCommerce sites, and if you have placed an order and in case payment gets failed there may be an option for retry payment which gets an internal call. So actually it checks if the payment call is a success or failure, in case if the payment gets failed it will not send a success message, instead it will call retry internally. Generally, in the real case, there will be a limit for retry call because suppose we are trying to call payment gateway and it was not responding for the first time them we made another retry attempt for making the payment with payment gateway. So actually if the site of the payment gateway is down we cannot retry for the whole day so we can set some limit for it. We need to write proper logic for handling retry to control the limit of call.
- See the below diagram, we have shown a simple flow where we are checking if the task is done or not.
- If the task is successfully done (example payment is done) just end the begin block.
- But in case if the task has not done or failed (example payment failed), in that case, we are redirecting our call to retry statement which will again call for the task from the same point.
- Inside the beginning loop, we can perform some operations like making any payment, etc.
- We have to write a condition hare to make a call to retry which we can do with the help of raise and rescue.
- In case of the condition if true or success than the exception will be raised(raise is a keyword to raise an exception in Ruby) and the rescue(rescue is a keyword in Ruby to catch an exception raised by raise statement) block will play its job.
- Inside the rescue block inside the diagram, the retry block will be written inside the rescue block. This means the raise statement will catch inside rescue and inside rescue, we will call to retry which will again call the task block.
- We need to take care a lot while using retry as if it will not be used with proper logic and understanding the loop can go for infinite as it will keep calling retry.
- Here retry should call with a proper limit, which means how many times it should call retry should be handled properly otherwise the loop can go for infinite.
See the below diagram for better understanding:
Examples to Implement Ruby retry
Below are the examples mentioned:
Example #1
Below is an example where we have taken any random number and run a loop on it to print the number, and if the number is greater than 7 raise an exception. The raised exception will be handled inside the rescue block. Inside the rescue block, we have commented on the retry statement, if we enable retry block then the loop will go for infinite time execution. As I have mentioned already we need to take care while using retry statements to avoid the infinite. Please follow the below example along with the screen of output.
Code:
numbers =10
numbers.times do |number|
begin
puts "This the iteration number inside begin block #{number}"
raise if number > 7
rescue
# Here we are using the retry to call again
puts "Here retry statement will be called as it falls under rescue block #{number}"
#retry
exit
end
end
Output:
Example #2
As I have mentioned to you that we need to use retry very carefully to avoid an infinite loop. Below is an example where once retry will get call the loop will go for infinite and no further execution of the code happens. We need to always check our requirements before using retry. Please see the below example along with the screen of the output.
Code:
begin
tryAgain ||= 0
puts "Please try again to execute the code block ##{ tryAgain }"
raise "We are raising exception each time to check tryAgain count"
rescue
#Here retry statement will handle if try again value higher then 2 it will start to retry and nothing will print:
puts "retry will get called"
retry if (tryAgain += 1) < 3
end
Output:
Conclusion
From this tutorial we learned about the retry of Ruby and learned about the retry of Ruby, we learned the places where the retry can be used in real life with the help of some important examples. We went through the many important situations where we can use and where we should avoid its uses.
Recommended Articles
We hope that this EDUCBA information on “Ruby retry” was beneficial to you. You can view EDUCBA’s recommended articles for more information.