Rescue Modifier (Inline Form) in Ruby
📖 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 occursUse the rescue modifier (inline form) exactly as
expression rescue valuePrint 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