As last week I went to Barcelona and didn’t have enough time in the weekend to properly prepare a larger topic, this week’s post will be short and concise. As a novelty, I will also layout this post in a sort of a record card that may be reused for other hook methods or even patterns in future posts. Lets go straight to the topic.

What are hook methods?

Hook methods are methods that if defined in the appropriate place, will be called by the Ruby interpreter on some event. You can use them to add custom behaviour when a particular event is fired. In this post we will take a look to the Inherited hook. Here is were the record card comes into play.

The inherited hook

Use cases

The inherited hook is used when a parent class must keep a record of its children subclasses. Some examples may be to keep track of:

Where should it be defined?

As a class method of the parent class to be subclassed.

When is it fired?

Whenever the parent class is subclassed.

Example

To see this hook method in action, lets implement a quick example to track payment method options.

When executed it produces:

That was it for the inherited hook method! Simple and useful. In the future I might write a post about Ruby’s hook methods and also might talk about other hooks methods in detail. If you want me to research on some topic of your interest please don’t hesitate to write a comment and let me now.