Ruby : module callbacks

We can override some callbacks methods in a module to modify the behaivor of a class, or the methods of a class:

If we wan to preform an action when a class “include” a module we need to override the method included in our module.

module Foo

  def self.included(mod)
    puts "and now  CallbackTest included has been used by #{mod.inspect} form Foo..."
  end

  def self.method_added(method)
    puts "Added #{method}"
  end 

end

If we want , for example, log the methods added to our class we must override method_added method.

class Bar
  include Foo

  def self.method_added(method)
    puts "Added #{method}"
  end 
end

class Bar
  def another_method
  end
end
Advertisements

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: