Complete the code to retry the block 3 times.
retry_count = 0 begin puts 'Trying...' raise 'Error' if retry_count < 2 rescue retry_count += 1 [1] end
The retry keyword restarts the begin block from the beginning.
Complete the code to retry only if the error message is 'Temporary failure'.
begin puts 'Attempting...' raise 'Temporary failure' rescue => e if e.message == [1] retry end end
The error message is a string, so it must be compared with double quotes "Temporary failure".
Fix the error in the retry logic to avoid infinite loop.
attempts = 0 begin attempts += 1 puts "Try #{attempts}" raise 'Fail' if attempts < 3 rescue if attempts < 3 [1] else puts 'Giving up' end end
retry restarts the begin block, allowing attempts to increase and eventually stop retrying.
Fill both blanks to retry only up to 5 times and print a message each retry.
max_retries = 5 tries = 0 begin tries += 1 puts "Attempt #{tries}" raise 'Error' if tries < max_retries rescue if tries < [1] puts 'Retrying...' [2] else puts 'No more retries' end end
Use max_retries to limit retries and retry to restart the block.
Fill all three blanks to retry only when error message is 'Timeout', up to 3 times, and print attempts.
max_attempts = 3 count = 0 begin count += 1 puts "Attempt #{count}" raise 'Timeout' if count < max_attempts rescue => e if e.message == [1] && count < [2] puts 'Retrying due to timeout' [3] else puts 'Stopping retries' end end
Compare error message with "Timeout", limit retries with max_attempts, and use retry to restart.