Ruby callbacks

This blog post is about ruby’s callbacks(hooks): what are the available ones,and how practically we can use them?


obj.method_missing(symbol [, *args] ) => result might be the most famous hook in ruby, and is being used a lot by ruby developers :

Invoked by Ruby when obj is sent a message it cannot handle. symbol is the symbol for the method called, and args are any arguments that were passed to it. By default, the interpreter raises an error when this method is called. However, it is possible to override the method to provide more dynamic behavior.

Here is a simple example where the user can mix colors using the mix method :

Now, how about letting the user do something like: ‘c.greenAndBlue’ or ‘c.grayAndYellow’ ?, let’s see how to do so:


const_missing : invoked when a reference is made to an undefined constant.

I will use it to show the user a more informative message when he tries to use a non existing constant:

included and extended

included and extended are fired when the module is included or extended :

However let’s move to a more practical use, if u look at the module documentation, you will find the following definition:

A Module is a collection of methods and constants. The methods in a module may be instance methods or module methods. Instance methods appear as methods in a class when the module is included, module methods do not.

Well, as you can see, you can’t include the module methods inside your class, then what to do?
I saw some people use a nice trick to do so, they split their module into 2 inner modules, one called InstanceMethods and the other called SingletonMethods, then they include the former, and extend the later, look at the following snippet of code :

method_added and singleton_method_added

method_added and singleton_method_added are another 2 callbacks that are fired when a new instance or singleton method is added.
In the code snippet bellow, I’m trying to prevent a developer from monkey patching(reopen) my colors class:

method_removed and method_undefined

method_removed: fired when an instance method is removed.
method_undefined: fired when an instance method is undefined.

singleton_method_removed and singleton_method_undefined

singleton_method_removed: fired when a singleton method is removed.
singleton_method_undefined: fired when a singleton method is undefined.


inherited is called when some class is being inherited:

A practical use will be in preventing subclasses from being created :

Well, that’s all for this post. The next post series will be on writing internal DSLs using ruby.
See you then…..

11 Replies to “Ruby callbacks”

  1. Khaled, I just discovered your blog and you are writing amazing technical posts. Great job and thanks for helping the Ruby community!

  2. Hi, nice post and neat clarity about what you are conveying. Keep up the good work and as Mathieu point out, thanks for helping the community! Looking forward to your upcoming posts on Ruby.

  3. Great post! 🙂

    I found it because I’m searching for an after_initialize class callback in Ruby. In other words, it should be fired after loading a class, any idea?

    Keep it up!

  4. Why did you use “self.send :mix, colors ,&block” instead of calling the function “normaly”.

    btw, thanks for the nice post 🙂

  5. @billy, cause mix is a Symbol there. It can’t be called directly. Another note, you can’t pass methods directly just like a one can do in Python or Javascript and then call it directly. The maximum u can do is to pass the name(symbol or string) and call it via send or eval methods, Or pass method object and call it from there.
    Anyway check this post on dynamic method invocation in Ruby.

  6. @José You can either override initialize if it’s a subclass, or you can define a new new method.

    class X
    class << self
    alias :_new, :new
    def new(*args)
    o = _new(*args)
    # … after initialize code …
    return o

  7. Interesting article. Note that near the end you have, “…inheritance is now allowed….”. I think you meant ‘not’.

Leave a Reply

Your email address will not be published. Required fields are marked *