What is the purpose of repeated and repeated statements in Ruby?

The only case I can think of for redois for operations such as writing to a socket or reading from a database, but if they are not executed once, subsequent attempts will most likely also fail, so it still seems a little pointless me, and as for retry, I canโ€™t think of any case when it would be useful.

It may seem pointless to me because I donโ€™t know or am not using Ruby, but I am trying to create an amazing language one day, so I would like to at least find out about the design of some of the most popular languages.

+5
source share
2 answers

, - , redo retry, , , , . redo, retry , . , , - - (, ), , , , retry begin...rescue. , , .

, , Net:SFTP. , . , . , , , , . :

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
+8

redo :

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
+6

All Articles