Bird
0
0

What is wrong with this hook usage?

medium📝 Debug Q7 of 15
Ruby - Advanced Metaprogramming
What is wrong with this hook usage?
class Framework
  def self.before_save(&block)
    @before_save = block
  end

  def self.run_before_save
    @before_save.call
  end
end

Framework.run_before_save
ADefining before_save as a class method
BCalling @before_save.call without checking if block exists
CUsing instance variable instead of class variable
DMissing block parameter in before_save
Step-by-Step Solution
Solution:
  1. Step 1: Check run_before_save method

    It calls @before_save.call without verifying if @before_save is set.
  2. Step 2: Identify potential error

    Since no block was assigned before calling run_before_save, this causes a nil error.
  3. Final Answer:

    Calling @before_save.call without checking if block exists -> Option B
  4. Quick Check:

    Always check block presence before calling = B [OK]
Quick Trick: Check if block exists before calling it [OK]
Common Mistakes:
  • Calling nil block without check
  • Confusing instance vs class variables
  • Assuming class method definition is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes