Bird
Raised Fist0
Rubyprogramming~10 mins

Retry for reattempting in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to retry the block 3 times.

Ruby
retry_count = 0
begin
  puts 'Trying...'
  raise 'Error' if retry_count < 2
rescue
  retry_count += 1
  [1]
end
Drag options to blanks, or click blank then click option'
Anext
Bretry
Cbreak
Dredo
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of 'retry' causes a syntax error.
Using 'break' exits the block instead of retrying.
2fill in blank
medium

Complete the code to retry only if the error message is 'Temporary failure'.

Ruby
begin
  puts 'Attempting...'
  raise 'Temporary failure'
rescue => e
  if e.message == [1]
    retry
  end
end
Drag options to blanks, or click blank then click option'
A"Temporary failure"
B"Permanent failure"
C'Temporary failure'
D'Permanent failure'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes causes mismatch.
Comparing with wrong error message string.
3fill in blank
hard

Fix the error in the retry logic to avoid infinite loop.

Ruby
attempts = 0
begin
  attempts += 1
  puts "Try #{attempts}"
  raise 'Fail' if attempts < 3
rescue
  if attempts < 3
    [1]
  else
    puts 'Giving up'
  end
end
Drag options to blanks, or click blank then click option'
Aredo
Bnext
Cbreak
Dretry
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'redo' causes infinite loop without incrementing attempts.
Using 'next' or 'break' causes syntax errors here.
4fill in blank
hard

Fill both blanks to retry only up to 5 times and print a message each retry.

Ruby
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
Drag options to blanks, or click blank then click option'
Amax_retries
Bretry
Ctries
Dbreak
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tries' instead of 'max_retries' causes wrong retry limit.
Using 'break' instead of 'retry' stops retries prematurely.
5fill in blank
hard

Fill all three blanks to retry only when error message is 'Timeout', up to 3 times, and print attempts.

Ruby
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
Drag options to blanks, or click blank then click option'
A"Timeout"
Bmax_attempts
Cretry
D"Error"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong error message string causes no retry.
Not limiting retries causes infinite loop.
Using 'break' instead of 'retry' stops retries.

Practice

(1/5)
1. What does the retry keyword do in Ruby when used inside a rescue block?
easy
A. It skips the current error and continues after the rescue block.
B. It restarts the code inside the begin block from the beginning.
C. It stops the program immediately without handling the error.
D. It logs the error and moves to the next line of code.

Solution

  1. Step 1: Understand retry usage context

    The retry keyword is used inside a rescue block to repeat the code in the begin block.
  2. Step 2: Effect of retry

    When an error occurs and retry is called, Ruby restarts the begin block from the start, trying again.
  3. Final Answer:

    It restarts the code inside the begin block from the beginning. -> Option B
  4. Quick Check:

    retry restarts code = A [OK]
Hint: Retry restarts the begin block after an error [OK]
Common Mistakes:
  • Thinking retry skips the error without retrying
  • Assuming retry stops the program
  • Confusing retry with error logging
2. Which of the following is the correct syntax to retry a block after rescuing an exception in Ruby?
easy
A. retry begin # code rescue end
B. begin # code retry rescue end
C. begin # code rescue retry end
D. begin retry # code rescue end

Solution

  1. Step 1: Identify correct retry placement

    The retry keyword must be inside the rescue block to reattempt the begin block.
  2. Step 2: Check syntax correctness

    begin # code rescue retry end places retry inside rescue, which is the correct syntax.
  3. Final Answer:

    begin # code rescue retry end -> Option C
  4. Quick Check:

    retry inside rescue = D [OK]
Hint: Retry must be inside rescue block after begin [OK]
Common Mistakes:
  • Placing retry outside rescue block
  • Putting retry before begin block
  • Using retry without rescue
3. What will be the output of this Ruby code?
attempts = 0
begin
  attempts += 1
  puts "Try #{attempts}"
  raise 'Error' if attempts < 3
rescue
  retry
end
puts 'Done'
medium
A. Try 1 Try 2 Try 3 Done
B. Try 1 Try 2 Done
C. Try 1 Try 2 Try 3 Try 4 Done
D. Try 1 Done

Solution

  1. Step 1: Trace attempts and retry behavior

    Initially, attempts = 0. Each try increments attempts and prints it. If attempts < 3, it raises an error and retries.
  2. Step 2: Count tries until no error

    Try 1: attempts=1, error raised, retry.
    Try 2: attempts=2, error raised, retry.
    Try 3: attempts=3, no error, exits retry loop.
  3. Step 3: Final output

    Prints "Try 1", "Try 2", "Try 3", then "Done" after loop ends.
  4. Final Answer:

    Try 1 Try 2 Try 3 Done -> Option A
  5. Quick Check:

    Retries until attempts=3 = B [OK]
Hint: Count retries until condition stops error [OK]
Common Mistakes:
  • Forgetting retry causes loop restart
  • Assuming fewer or more tries than 3
  • Ignoring the final puts after retry
4. Identify the error in this Ruby code using retry:
begin
  puts 'Start'
  raise 'Oops'
rescue
  puts 'Error caught'
  retry
  puts 'After retry'
end
medium
A. The puts 'After retry' statement is unreachable after retry.
B. retry cannot be used inside rescue block.
C. Missing end for begin block.
D. The raise statement should be inside rescue.

Solution

  1. Step 1: Analyze retry placement

    The retry statement causes the code to jump back to begin, so any code after retry in rescue is never run.
  2. Step 2: Identify unreachable code

    The line puts 'After retry' is after retry and will never execute, making it unreachable.
  3. Final Answer:

    The puts 'After retry' statement is unreachable after retry. -> Option A
  4. Quick Check:

    Code after retry is unreachable = A [OK]
Hint: Code after retry in rescue is never executed [OK]
Common Mistakes:
  • Thinking retry can't be inside rescue
  • Ignoring unreachable code warnings
  • Misplacing raise statement
5. You want to retry a block up to 3 times if an error occurs, then stop retrying. Which code correctly implements this in Ruby?
hard
A. attempts = 0 begin # risky code attempts += 1 rescue retry if attempts < 3 end
B. attempts = 0 begin attempts += 1 # risky code rescue retry end
C. attempts = 0 begin # risky code rescue retry unless attempts > 3 end
D. attempts = 0 begin # risky code rescue attempts += 1 retry if attempts < 3 end

Solution

  1. Step 1: Understand retry with counter

    We must increment attempts inside rescue to count retries after errors.
  2. Step 2: Check retry condition

    Retry only if attempts < 3 to limit retries to 3 times.
  3. Step 3: Verify code correctness

    attempts = 0 begin # risky code rescue attempts += 1 retry if attempts < 3 end increments attempts in rescue and retries if attempts < 3, correctly limiting retries.
  4. Final Answer:

    attempts = 0 begin # risky code rescue attempts += 1 retry if attempts < 3 end -> Option D
  5. Quick Check:

    Increment attempts in rescue before retry = C [OK]
Hint: Increment attempts in rescue before retry condition [OK]
Common Mistakes:
  • Incrementing attempts before begin block
  • Retrying without limiting attempts
  • Using wrong retry condition logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard