Module#include (no need for ClassMethods)

If we define Module#include as:

class Module

alias _include include

def include(*mods)
  mods.each do |mod|
    if mod.const_defined?(:Self)
      extend mod::Self
      # pass it along if module
      if instance_of?(Module)
        const_set(:Self, Module.new) unless const_defined?(:Self)
        const_get(:Self).send(:include, mod::Self)
      end
    end
  end
  _include(*mods)
end

end

Example:

module X
def x; “x”; end

module Self
  def q; "q"; end
end

end

class C
include X
end

C.q #=> “q”

Benefits:

  • Documents well in RDoc
  • Fairly concise and clear
  • Self module fits in well with “class << self”
  • No callbacks used.

T.

On Dec 9, 11:24 pm, Trans [email protected] wrote:

      # pass it along if module

class C

  • No callbacks used.

T.

Clever. :slight_smile: It doesn’t seem to grok class vars inside of ::Self (?),
but class/module_eval works. Weird since class methods work without
the eval.

Regards,
Jordan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs