0
0
Rubyprogramming~10 mins

Rescue modifier (inline form) 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 return 0 if the division raises an error.

Ruby
result = 10 / 0 [1] 0
Drag options to blanks, or click blank then click option'
Arescue
Bretry
Censure
Dbegin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'retry' instead of 'rescue' causes syntax errors.
Using 'ensure' does not handle exceptions inline.
2fill in blank
medium

Complete the code to safely convert a string to integer, returning 0 if it fails.

Ruby
number = Integer('abc') [1] 0
Drag options to blanks, or click blank then click option'
Abegin
Bensure
Celse
Drescue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'begin' or 'ensure' inline causes syntax errors.
Not handling exceptions leads to program crash.
3fill in blank
hard

Fix the error in the code to return 'error' if the method call fails.

Ruby
value = some_method [1] 'error'
Drag options to blanks, or click blank then click option'
Aretry
Brescue
Craise
Densure
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'retry' causes infinite loops.
Using 'raise' re-raises the exception.
4fill in blank
hard

Fill both blanks to return the length of a string or 0 if it is nil.

Ruby
length = str[1].length [2] 0
Drag options to blanks, or click blank then click option'
A&
Brescue
C&.
D||
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' instead of safe navigation '&.'.
Using 'rescue' instead of '||' returns nil if str is nil.
5fill in blank
hard

Fill all three blanks to safely parse a number or return -1 if it fails.

Ruby
result = (input[1].to_i) [2] -1 [3] -1
Drag options to blanks, or click blank then click option'
A&.
Brescue
C||
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' instead of '&.'.
Not using rescue causes crashes.
Misusing '||' changes logic.