Overview

15 Callbacks, hooks, and runtime introspection

Ruby provides a rich set of runtime hooks and reflective tools that let programs react to events as they happen and inspect their own structure while running. Callbacks such as method_missing, included, extended, inherited, const_missing, method_added, and singleton_method_added can be used to intercept missing methods, module mix-ins, extensions, subclassing, unresolved constants, and newly defined methods. These hooks are per class or per object, making them precise tools for shaping behavior without adding a lot of boilerplate.

The chapter then explores how Ruby lets you query capabilities at runtime. Using methods like methods, private_methods, protected_methods, instance_methods, public_instance_methods, singleton_methods, and their variants, you can inspect what an object, class, or module can do, and even compare method lists to discover overrides or inheritance relationships. The text emphasizes filtering and combining these lists to answer practical questions, such as which bang methods lack non-bang counterparts or which classes override methods from Enumerable.

Finally, the chapter shows how Ruby can reveal what variables are in scope and how execution arrived at a given point. You can inspect local, global, instance, class, and constant-related state, and use caller or caller_locations to work with stack traces in either string form or as structured location objects. The chapter culminates in a small test framework that uses callbacks, method inspection, and stack introspection to automatically run test methods, execute setup code, and report assertion failures with useful location information, demonstrating how these features can be combined into practical metaprogramming.

Summary

  • method_missing, along with respond_to? and respond_to_missing?, offers a versatile toolset for handling unrecognized messages dynamically.
  • Runtime callbacks for classes and modules include inherited, extended, and included.
  • If defined, method_added and singleton_method_added are called when those events take place.
  • Objects can be queried about their available methods, based on access level (public, for example) and whether the methods are singleton methods or not.
  • Defining const_missing allows interception of unknown constant references.
  • Stack traces provide information about where in the code a given event occurred, and can be intelligently queried through the use of caller_locations. .
  • Taken together, Ruby’s callback and runtime introspection facilities teach you a lot about the language, while also providing a practical and powerful programming toolset.

FAQ

What are Ruby callbacks and hooks in this chapter?Ruby callbacks and hooks are special methods with reserved names that Ruby calls automatically when certain runtime events happen, such as including a module, inheriting from a class, or calling an undefined method.
How does method_missing help handle unknown method calls?method_missing is invoked when an object receives a message for a method it does not define. You can use it to intercept the call, delegate it elsewhere, or implement dynamic behavior instead of raising NoMethodError.
Why should respond_to_missing? be defined when using method_missing?respond_to_missing? makes respond_to? report dynamic methods correctly. Without it, an object may be able to handle a message through method_missing but still claim it does not respond to that method.
What is the purpose of the included callback in a module?The included callback runs when a module is included in a class or another module. It can be used to run setup code, add class methods, or customize the including class.
How is extended different from included?extended is triggered when a module is extended onto an object. While included is about adding instance methods through mix-ins, extended is about adding a module’s methods to a single object’s singleton class.
What does inherited do in a class?inherited is a class method callback that runs when the class is subclassed. It receives the subclass as its argument, allowing the parent class to react to inheritance events.
How does const_missing work?const_missing is called when Ruby encounters an undefined constant within a class or module. It lets you handle the missing constant dynamically, such as defining it on the fly or providing a fallback value.
What do methods, private_methods, and protected_methods return?These methods return arrays of method names as symbols. methods lists public and protected methods, private_methods lists private methods, and protected_methods lists protected methods.
What is the difference between singleton_methods and class instance methods?singleton_methods lists methods defined on a specific object’s singleton class. For class objects, those singleton methods are the class methods. Class instance methods, by contrast, are methods available to instances of the class.
What is the difference between caller and caller_locations?caller returns a stack trace as an array of strings. caller_locations returns structured backtrace objects, which makes it easier to inspect details like file path, line number, and method label.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Well-Grounded Rubyist, Fourth Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Well-Grounded Rubyist, Fourth Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Well-Grounded Rubyist, Fourth Edition ebook for free