Bird
0
0

Which of the following is the correct syntax to define an included hook inside a Ruby module?

easy📝 Syntax Q12 of 15
Ruby - Advanced Metaprogramming

Which of the following is the correct syntax to define an included hook inside a Ruby module?

module Example
  # What goes here?
end
Adef self.included(base) puts "Included in #{base}" end
Bdef included(base) puts "Included in #{base}" end
Cdef included puts "Included" end
Ddef self.include(base) puts "Included in #{base}" end
Step-by-Step Solution
Solution:
  1. Step 1: Recall how to define class methods in modules

    The included hook must be a module method, so it needs to be defined with self.included.
  2. Step 2: Check the method signature

    The method receives the including class as a parameter, so it must accept one argument (base).
  3. Final Answer:

    def self.included(base) puts "Included in #{base}" end -> Option A
  4. Quick Check:

    included hook = def self.included(base) [OK]
Quick Trick: Use def self.included(base) for included hook syntax [OK]
Common Mistakes:
  • Defining included without self (instance method instead of module method)
  • Missing the base parameter
  • Using def self.include instead of included

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes