What is the purpose of redo and retry statements in Ruby?
Asked Answered
C

2

7

The only use case I can think of for redo would be for operations like writing to a socket or reading from a database, but if these fail once, subsequent attempts will most likely also fail so it still seems a bit pointless to me and as for retry I can't really think of any case where it would be useful.

This might only seem meaningless to me since I don't know or use Ruby, but I aspire to create an awesome language one day so I would like to at least know the reasoning behind the design of some of the most popular languages out there.

Catastrophe answered 8/4, 2012 at 13:29 Comment(1)
Your assertion that "subsequent attempts will most likely also fail" seems highly debatable. Seems to me that this would be very dependant on what has failed and why. For example, if the reason why your database operation file was a deadlock caused by the query someone else was running then backing off and retrying is exactly the right thing to do.Logorrhea
S
9

The idea is that you change something before calling redo or retry, in the hopes that the whatever you were doing will work the second time. I don't have an example for redo, but we have found uses for retry in the application I'm working on. Basically, if you have a bit of code that might fail due to something external (e.g. network), but performing a precondition check every time you run the code would be too expensive, you can use retry in a begin...rescue block. Not sure if that was clear, so I'll get right to the example.

Basically, we have some code that accesses a remote directory using Net:SFTP. The directory should exist, but in some exceptional cases it will not have been made yet. If it's not there, we want to try once to make it. But performing the network access to check if the directory exists every time would be too expensive, especially since it's only in exceptional cases that it won't be there. So we do it as follows:

tried_mkdir = false
begin
  # Attempt to access remote directory
  ...
rescue Net::SFTP::StatusException
  raise if tried_mkdir
  tried_mkdir = true
  # Attempt to make the remote directory
  ...
  retry
end
Siegfried answered 8/4, 2012 at 16:31 Comment(0)
P
7

Use case for redo could be user input checking:

nums = Array.new(5){[rand(1..9), rand(1..9)]}
nums.each do |num1, num2|
  print "What is #{num1} + #{num2}: "
  redo unless gets.to_i == num1 + num2
end
Plectrum answered 8/4, 2012 at 19:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.