MΓ³dulo 27 Β· Metaprogramming & Rails Magic β LecciΓ³n 5 de 6 Β· ~9 min
Hooks & Concerns
Ruby tells you when things happen to a class
Classes and modules have lifecycle hooks β methods Ruby calls automatically at key moments:
included(base)β fired when a module isincluded intobase.extended(base)β fired when a moduleextends an object.inherited(subclass)β fired when a class is subclassed.method_added(name)β fired when a method is defined.
These are the seams Rails hooks into to inject behavior at exactly the right moment. included is the star: it's how a module can add both instance methods and class methods when mixed in.
In an interview, say: "
included,inherited, and friends are runtime hooks Ruby fires during class construction. Rails usesincludedto inject class-level macros when a concern is mixed in β that's the whole mechanism behindActiveSupport::Concern."