Bird
0
0

You want to build a Ruby framework that allows users to add multiple hooks for before_save and run them all in order before saving. Which approach best supports this?

hard📝 Application Q15 of 15
Ruby - Advanced Metaprogramming
You want to build a Ruby framework that allows users to add multiple hooks for before_save and run them all in order before saving. Which approach best supports this?
AStore hooks in an array and call each block in sequence before saving
BStore a single block in a class variable and overwrite it each time
CUse global variables to store hooks and call them randomly
DDefine multiple methods named before_save and rely on Ruby to call all
Step-by-Step Solution
Solution:
  1. Step 1: Understand the need for multiple hooks

    To support many hooks, you must keep all hook blocks, not overwrite them.
  2. Step 2: Choose a data structure to hold hooks

    An array can store multiple blocks, allowing iteration and calling each in order.
  3. Step 3: Why other options fail

    Overwriting loses hooks, global variables cause conflicts, and Ruby does not call multiple methods with same name automatically.
  4. Final Answer:

    Store hooks in an array and call each block in sequence before saving -> Option A
  5. Quick Check:

    Multiple hooks need array storage = A [OK]
Quick Trick: Use array to keep and run multiple hooks in order [OK]
Common Mistakes:
  • Overwriting hooks instead of storing all
  • Using global variables causing conflicts
  • Expecting Ruby to call duplicate method names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes