📖 Scenario: Imagine you are writing a small program that tries to convert user input into a number. Sometimes, the input might not be a valid number, and your program should handle that gracefully without crashing.
🎯 Goal: You will build a Ruby program that uses the rescue modifier (inline form) to handle errors when converting strings to integers.
📋 What You'll Learn
Create a variable called user_input with the exact string value "abc"
Create a variable called number that tries to convert user_input to an integer using Integer(user_input) but uses the rescue modifier to assign 0 if an error occurs
Use the rescue modifier (inline form) exactly as expression rescue value
Print the value of number using puts
💡 Why This Matters
🌍 Real World
When accepting user input, programs often need to handle invalid data without crashing. Rescue modifier helps keep programs running smoothly by providing fallback values.
💼 Career
Understanding error handling is important for writing robust Ruby applications, especially in web development and scripting.
Progress0 / 4 steps
1
Create the user input variable
Create a variable called user_input and set it to the string "abc" exactly.
Ruby
Hint
Use = to assign the string "abc" to user_input.
2
Use rescue modifier to convert input
Create a variable called number that tries to convert user_input to an integer using Integer(user_input) but uses the rescue modifier (inline form) to assign 0 if an error occurs. Use the exact syntax: Integer(user_input) rescue 0.
Ruby
Hint
Use Integer(user_input) rescue 0 to handle invalid input gracefully.
3
Try with a valid number string
Change the value of user_input to the string "123" exactly. Keep the number assignment the same using the rescue modifier.
Ruby
Hint
Just replace the string value assigned to user_input with "123".
4
Print the result
Use puts to print the value of the variable number.
Ruby
Hint
Use puts number to display the number.
Practice
(1/5)
1. What does the rescue modifier do in Ruby when used like value = 10 / x rescue 0?
easy
A. It ignores the division and returns nil
B. It always returns 10 divided by x
C. It returns 0 if dividing by x causes an error
D. It raises an error if x is zero
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
If 10 / x raises 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 C
Quick Check:
rescue modifier returns fallback on error [OK]
Hint: Rescue modifier returns fallback if error happens [OK]
Common Mistakes:
Thinking rescue modifier prevents all errors
Assuming it always returns the original expression
Confusing rescue modifier with if-else
2. Which of the following is the correct syntax for using the rescue modifier inline in Ruby?
easy
A. result = some_method rescue fallback_value
B. result = rescue some_method fallback_value
C. result = some_method ? rescue fallback_value
D. result = some_method else rescue fallback_value
Solution
Step 1: Recall the rescue modifier syntax
The rescue modifier is placed after an expression, followed by the fallback value, like expression 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 A