Challenge - 5 Problems
Retry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this retry example?
Consider the following Ruby code using
retry. What will it print?Ruby
count = 0 begin count += 1 puts "Attempt #{count}" raise 'fail' if count < 3 rescue retry end puts 'Done'
Attempts:
2 left
💡 Hint
The retry keyword restarts the begin block after rescue.
✗ Incorrect
The code increments count each time. It raises an error until count reaches 3. Each rescue triggers retry, so it prints 'Attempt' three times before finishing.
❓ Predict Output
intermediate1:30remaining
What happens if retry is used outside rescue?
What error does this Ruby code produce?
Ruby
retry
Attempts:
2 left
💡 Hint
retry can only be used inside rescue blocks.
✗ Incorrect
Using retry outside rescue causes a LocalJumpError because Ruby expects retry only in rescue context.
🔧 Debug
advanced2:30remaining
Why does this retry loop never end?
Look at this code. Why does it print 'Trying...' forever?
Ruby
attempts = 0 begin attempts = 0 attempts += 1 puts 'Trying...' raise 'error' rescue retry if attempts < 5 end puts 'Finished'
Attempts:
2 left
💡 Hint
Check if attempts changes between retries.
✗ Incorrect
Every time `retry` is executed, it restarts the `begin` block from the top. The `attempts = 0` inside the begin block resets the counter each time, so `attempts` becomes 1 after increment and is always < 5 when checked, causing infinite retries.
📝 Syntax
advanced1:30remaining
Which option causes a syntax error related to retry?
Which of these Ruby snippets will cause a syntax error?
Attempts:
2 left
💡 Hint
retry must be inside rescue block, not before it.
✗ Incorrect
Option D uses retry inside begin block before rescue, which is invalid syntax. retry must be inside rescue block.
🚀 Application
expert3:00remaining
How many times does this retry block execute?
Given this Ruby code, how many times will 'Processing...' be printed?
Ruby
count = 0 begin count += 1 puts 'Processing...' raise 'error' if count < 4 rescue retry end
Attempts:
2 left
💡 Hint
Does the retry condition ever stop?
✗ Incorrect
The `raise` only happens if `count < 4`. It prints and raises for count=1,2,3 (retrying each time), then for count=4 prints but does not raise, skipping rescue and exiting the block. Thus, printed 4 times.