Ruby retry

Ruby retry
Techiio-author
Written by Sagar RabidasJanuary 6, 2022
7 min read
Ruby
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Ruby retry.

Ruby:-

Ruby is an open-source object-oriented scripting language invented in the mid-90s by Yukihiro Matsumoto.

Unlike languages such as C and C++, a scripting language doesn’t talk directly to hardware. It’s written to a text file and then parsed by an interpreter and turned into code. These programs are generally procedural, meaning they are read from top to bottom.

Object-oriented languages, on the other hand, break out pieces of code into objects that can be created and used as needed. You can reuse these objects in other parts of the program or even other applications.

blogpost

Ruby retry:-

In ruby, retry assertion permit us to repeat the entire loop from the beginning, this new for programming hints, ordinarily, retry statement might be used in case if we're running on any transaction or another kind of looping wherein each generation of a loop has to achieve success or in some precise sequences, in case if we located any trouble with any generation of the going for walks loop we will use the retry announcement to begin the loop from the beginning, retry declaration is continually used within the loop this means that there need to be any generation for implementation of the retry announcement.

Syntax:-

In the below we have written a simple syntax for the 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 that will check for any particular condition according to our requirements.
  • In case of the condition is 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

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

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 than 2 it will start to retry and nothing will print:

puts "retry will get called"
retry if (tryAgain += 1) < 3
end

ruby
ruby retry
loops
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Sagar Rabidas
Software Developer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+8 more
300 Blog Posts
14 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance