Complete the code to return 0 if the division raises an error.
result = 10 / 0 [1] 0
The rescue modifier catches exceptions inline and returns the value after it if an error occurs.
Complete the code to safely convert a string to integer, returning 0 if it fails.
number = Integer('abc') [1] 0
The rescue modifier catches the exception from Integer('abc') and returns 0.
Fix the error in the code to return 'error' if the method call fails.
value = some_method [1] 'error'
The rescue modifier catches exceptions and returns the fallback value.
Fill both blanks to return the length of a string or 0 if it is nil.
length = str[1].length [2] 0
The safe navigation operator &. returns nil if str is nil without raising an error. The || operator returns 0 if the result is nil.
Fill all three blanks to safely parse a number or return -1 if it fails.
result = (input[1].to_i) [2] -1 [3] -1
The safe navigation operator &. calls to_i only if input is not nil. The rescue modifier catches exceptions and returns -1. The || operator returns -1 if the left side is false or nil.