What if you could stop your program from crashing with just a tiny addition to your code?
Why Rescue modifier (inline form) in Ruby? - Purpose & Use Cases
Imagine you have a program that tries to divide numbers, but sometimes the divisor might be zero. Without a quick way to handle errors, your program crashes and stops working.
Manually writing full error handling blocks everywhere makes your code long and hard to read. It's easy to forget to handle some errors, and the program can break unexpectedly.
The rescue modifier lets you handle errors right where they happen, in a short and clear way. It keeps your code clean and prevents crashes by providing a quick fallback value.
begin result = 10 / divisor rescue ZeroDivisionError result = 0 end
result = 10 / divisor rescue 0
You can write safer, simpler code that keeps running smoothly even when unexpected errors happen.
When reading user input that might be empty or invalid, you can quickly provide a default value without stopping the whole program.
Manual error handling can be long and repetitive.
Rescue modifier handles errors inline, making code shorter and clearer.
It helps programs keep running smoothly by providing quick fallback values.
Practice
value = 10 / x rescue 0?Solution
Step 1: Understand the rescue modifier usage
The rescue modifier catches exceptions raised by the expression before it and returns the value after rescue if an error occurs.Step 2: Apply to the example
If10 / xraises an error (like division by zero), the rescue modifier returns 0 instead of crashing.Final Answer:
It returns 0 if dividing by x causes an error -> Option CQuick Check:
rescue modifier returns fallback on error [OK]
- Thinking rescue modifier prevents all errors
- Assuming it always returns the original expression
- Confusing rescue modifier with if-else
Solution
Step 1: Recall the rescue modifier syntax
The rescue modifier is placed after an expression, followed by the fallback value, likeexpression rescue fallback.Step 2: Match with options
Only result = some_method rescue fallback_value matches this syntax correctly:result = some_method rescue fallback_value.Final Answer:
result = some_method rescue fallback_value -> Option AQuick Check:
Correct rescue modifier syntax = result = some_method rescue fallback_value [OK]
- Placing rescue before the expression
- Using question marks or else with rescue modifier
- Confusing rescue modifier with rescue block
value = Integer('abc') rescue 'error'
puts valueSolution
Step 1: Understand Integer conversion with invalid string
CallingInteger('abc')raises an ArgumentError because 'abc' is not a valid number.Step 2: Apply rescue modifier
The rescue modifier catches this error and returns the string 'error' instead of crashing.Final Answer:
error -> Option BQuick Check:
Invalid Integer conversion rescued to 'error' [OK]
- Expecting the original string as output
- Thinking rescue returns nil by default
- Assuming code raises exception without rescue
result = 10 / 0 rescue puts 'Error!'
Solution
Step 1: Analyze rescue modifier with puts
The rescue modifier catches the division by zero error and executesputs 'Error!', which prints the message.Step 2: Understand return value of puts
putsreturns nil, soresultis assigned nil after rescue.Final Answer:
No error; prints 'Error!' and result is nil -> Option AQuick Check:
Rescue modifier runs puts and assigns nil [OK]
- Expecting rescue modifier to return printed string
- Thinking rescue modifier causes syntax error here
- Assuming result keeps original expression value
Solution
Step 1: Understand rescue modifier placement
The rescue modifier must come after the expression to catch exceptions and provide fallback.Step 2: Check options for correct syntax and logic
Only num = Integer(user_input) rescue -1 correctly places rescue afterInteger(user_input)and returns -1 on failure.Final Answer:
num = Integer(user_input) rescue -1 -> Option DQuick Check:
Correct rescue modifier usage for fallback = num = Integer(user_input) rescue -1 [OK]
- Placing rescue before expression
- Using invalid syntax with question marks or else
- Forgetting rescue modifier for error handling
Ready to prove it?
Pass the test and this pattern moves unknown
