Extending a Ruby module in another module, including the module methods

If you are the author of module A and will frequently need this, you can re-author A like so:

module A 
  module ClassMethods
    def say_hi
      puts "hi"
    end
  end
  extend ClassMethods
  def self.included( other )
    other.extend( ClassMethods )
  end
end

module B 
  include A
end

A.say_hi #=> "hi"
B.say_hi #=> "hi" 

Leave a Comment