Consider the following extension (a template popularized by several Rails plugins over the years):
module Extension
def self.included(recipient)
recipient.extend ClassMethods
recipient.send :include, InstanceMethods
end
module ClassMethods
def macro_method
puts "Called macro_method within #{self.name}"
end
end
module InstanceMethods
def instance_method
puts "Called instance_method within #{self.object_id}"
end
end
end
If you want to expose this for each class, you can do the following:
Object.send :include, Extension
Now you can define any class and use the macro method:
class FooClass
macro_method
end
And instances can use instance methods:
FooClass.new.instance_method
But even if Module.is_a?(Object), you cannot use the macro method in the module.
module FooModule
macro_method
end
This is true even if you explicitly include the source Extensionin Modules Module.send(:include, Extension).
For individual modules, you can enable extensions manually and get the same effect:
module FooModule
include Extension
macro_method
end
But how do you add macro-like methods to all Ruby modules?