0
0
Rubyprogramming~10 mins

Ensure for cleanup in Ruby - Interactive Code Practice

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

Complete the code to ensure the cleanup message is always printed.

Ruby
begin
  puts 'Start'
[1]
end
Drag options to blanks, or click blank then click option'
Aelse puts 'Cleanup'
Bensure puts 'Cleanup'
Crescue puts 'Cleanup'
Dfinally puts 'Cleanup'
Attempts:
3 left
💡 Hint
Common Mistakes
Using rescue instead of ensure
Using else instead of ensure
Using finally which is not Ruby syntax
2fill in blank
medium

Complete the code to print 'Cleanup done' regardless of errors.

Ruby
begin
  puts 'Processing'
  raise 'Error occurred'
[1]
end
Drag options to blanks, or click blank then click option'
Aelse puts 'No errors'
Brescue puts 'Error handled'
Censure puts 'Cleanup done'
Dfinally puts 'Cleanup done'
Attempts:
3 left
💡 Hint
Common Mistakes
Using rescue to print cleanup message
Using else which runs only if no error
Using finally which is not valid Ruby
3fill in blank
hard

Fix the error in the code to ensure cleanup always runs.

Ruby
begin
  puts 'Running'
[1]
  puts 'Done'
end
Drag options to blanks, or click blank then click option'
Aensure puts 'Cleanup'
Brescue puts 'Cleanup'
Celse puts 'Cleanup'
Dfinally puts 'Cleanup'
Attempts:
3 left
💡 Hint
Common Mistakes
Using rescue which runs only on errors
Using else which runs only if no error
Using finally which is not Ruby syntax
4fill in blank
hard

Fill both blanks to print 'Start', handle errors, and always print 'Cleanup'.

Ruby
begin
  puts 'Start'
  raise 'Oops'
[1]
[2]
end
Drag options to blanks, or click blank then click option'
Arescue puts 'Error handled'
Bensure puts 'Cleanup'
Celse puts 'No error'
Dfinally puts 'Cleanup'
Attempts:
3 left
💡 Hint
Common Mistakes
Using else instead of rescue
Using finally which is invalid
Swapping rescue and ensure order
5fill in blank
hard

Fill all three blanks to handle errors, print success message, and always cleanup.

Ruby
begin
  puts 'Try block'
  raise 'Fail'
[1]
[2]
[3]
end
Drag options to blanks, or click blank then click option'
Arescue puts 'Error caught'
Belse puts 'Success'
Censure puts 'Cleanup done'
Dfinally puts 'Cleanup done'
Attempts:
3 left
💡 Hint
Common Mistakes
Using finally which is invalid
Omitting else block
Placing ensure before rescue