Skip to content

Instantly share code, notes, and snippets.

@xpepper
Last active December 16, 2015 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xpepper/5375674 to your computer and use it in GitHub Desktop.
Save xpepper/5375674 to your computer and use it in GitHub Desktop.
Reimplementing Module#include method

Reimplementing Module#include method (my_include method):

  1. my_include takes one or many modules
  2. it goes on a reverse traversal and for each module it adds its features to self
  3. calls an home-made hook method
class Module
  def my_include(*modules)
    modules.reverse_each do |each_module|
      each_module.send(:append_features, self)
      each_module.send(:my_included, self)
    end
  end
  
  def my_included(base)
    # void implementation
  end
end

module OneModule
  def self.my_included(base)
    puts ".........including from #{base}"
  end
  def ciao
    puts "ciao!"
  end
end


class MyClass
  my_include OneModule
end


c = MyClass.new
c.ciao
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment