Bird
Raised Fist0
Rubyprogramming~5 mins

Rescue modifier (inline form) in Ruby - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the rescue modifier (inline form) in Ruby?
It is a way to handle exceptions in a single line using the rescue keyword after an expression. It returns the expression's value if no error occurs, or the rescue value if an error happens.
Click to reveal answer
beginner
How does the rescue modifier differ from a full begin-rescue-end block?
The rescue modifier is shorter and used inline after an expression, while begin-rescue-end is a block that can handle multiple lines and more complex error handling.
Click to reveal answer
beginner
Example: What does result = 10 / 0 rescue 'error' return?
It returns the string 'error' because dividing by zero raises an exception, which is caught by the rescue modifier.
Click to reveal answer
intermediate
Can the rescue modifier catch specific exception types?
No, the rescue modifier catches all StandardError exceptions by default. For specific exceptions, use a full begin-rescue block.
Click to reveal answer
beginner
Why use the rescue modifier instead of a full rescue block?
It makes code shorter and cleaner for simple error handling when you want to provide a fallback value quickly.
Click to reveal answer
What does the rescue modifier do in Ruby?
AHandles exceptions inline and returns a fallback value if an error occurs
BDefines a new method
CCreates a loop
DDeclares a variable
Which of these is a correct use of the rescue modifier?
Arescue value = 5 / 0 'fail'
Bvalue = 5 / 0 rescue 'fail'
Cbegin value = 5 / 0 rescue 'fail' end
Dvalue = rescue 5 / 0 'fail'
If no error occurs, what does the rescue modifier return?
AThe original expression's value
BThe rescue value
Cnil
DAn error message
Can the rescue modifier specify which exception to catch?
ANo, it catches all exceptions including system errors
BYes, by adding the exception name after rescue
CYes, by using a block
DNo, it catches all StandardError exceptions
Which is a benefit of using the rescue modifier?
AHandles multiple exceptions separately
BAllows retrying the code automatically
CShorter code for simple error handling
DLogs errors to a file
Explain how the rescue modifier works in Ruby and when you might use it.
Think about how to handle errors quickly in one line.
You got /4 concepts.
    Write a Ruby expression using the rescue modifier to divide two numbers and return 'error' if division fails.
    Use the rescue keyword after the division.
    You got /3 concepts.

      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

      1. 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.
      2. Step 2: Apply to the example

        If 10 / x raises an error (like division by zero), the rescue modifier returns 0 instead of crashing.
      3. Final Answer:

        It returns 0 if dividing by x causes an error -> Option C
      4. 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

      1. Step 1: Recall the rescue modifier syntax

        The rescue modifier is placed after an expression, followed by the fallback value, like expression rescue fallback.
      2. Step 2: Match with options

        Only result = some_method rescue fallback_value matches this syntax correctly: result = some_method rescue fallback_value.
      3. Final Answer:

        result = some_method rescue fallback_value -> Option A
      4. Quick Check:

        Correct rescue modifier syntax = result = some_method rescue fallback_value [OK]
      Hint: Rescue modifier goes after expression, then fallback [OK]
      Common Mistakes:
      • Placing rescue before the expression
      • Using question marks or else with rescue modifier
      • Confusing rescue modifier with rescue block
      3. What is the output of this Ruby code?
      value = Integer('abc') rescue 'error'
      puts value
      medium
      A. abc
      B. error
      C. 0
      D. Raises an exception

      Solution

      1. Step 1: Understand Integer conversion with invalid string

        Calling Integer('abc') raises an ArgumentError because 'abc' is not a valid number.
      2. Step 2: Apply rescue modifier

        The rescue modifier catches this error and returns the string 'error' instead of crashing.
      3. Final Answer:

        error -> Option B
      4. Quick Check:

        Invalid Integer conversion rescued to 'error' [OK]
      Hint: Rescue returns fallback when conversion fails [OK]
      Common Mistakes:
      • Expecting the original string as output
      • Thinking rescue returns nil by default
      • Assuming code raises exception without rescue
      4. Identify the error in this Ruby code snippet using rescue modifier:
      result = 10 / 0 rescue puts 'Error!'
      medium
      A. No error; prints 'Error!' and result is nil
      B. Syntax error because rescue modifier cannot follow puts
      C. Runtime error because rescue modifier is misplaced
      D. result will be 0 without error

      Solution

      1. Step 1: Analyze rescue modifier with puts

        The rescue modifier catches the division by zero error and executes puts 'Error!', which prints the message.
      2. Step 2: Understand return value of puts

        puts returns nil, so result is assigned nil after rescue.
      3. Final Answer:

        No error; prints 'Error!' and result is nil -> Option A
      4. Quick Check:

        Rescue modifier runs puts and assigns nil [OK]
      Hint: Rescue modifier returns last expression value after rescue [OK]
      Common Mistakes:
      • Expecting rescue modifier to return printed string
      • Thinking rescue modifier causes syntax error here
      • Assuming result keeps original expression value
      5. You want to safely convert user input to integer but return -1 if conversion fails. Which code correctly uses rescue modifier for this?
      hard
      A. num = Integer(user_input) else rescue -1
      B. num = rescue Integer(user_input) -1
      C. num = Integer(user_input) ? rescue -1
      D. num = Integer(user_input) rescue -1

      Solution

      1. Step 1: Understand rescue modifier placement

        The rescue modifier must come after the expression to catch exceptions and provide fallback.
      2. Step 2: Check options for correct syntax and logic

        Only num = Integer(user_input) rescue -1 correctly places rescue after Integer(user_input) and returns -1 on failure.
      3. Final Answer:

        num = Integer(user_input) rescue -1 -> Option D
      4. Quick Check:

        Correct rescue modifier usage for fallback = num = Integer(user_input) rescue -1 [OK]
      Hint: Put rescue after expression, then fallback value [OK]
      Common Mistakes:
      • Placing rescue before expression
      • Using invalid syntax with question marks or else
      • Forgetting rescue modifier for error handling
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard