class Module
Extend class Module
to support additional “include” functionality.
Public Instance Methods
Include additional modules.
Unlike a traditional include, the modules’ ::included methods (if present) will be called when the current module is included if they have not already been previously included by the including object’s ancestors.
Another difference is that multiple modules are always included first-to-last, so it doesn’t matter if you “extended_include M1, M2, M3” or “extended_include M1; extended_include
M2; extended_include
M3” or any other variant with the same reference order. Methods will always be sought in last-to-first order (M3, M2, M1).
# File lib/extended_include.rb, line 92 def extended_include (*modules) Extended_Include.add_includes self, *modules end
Extend class methods into the including object when including this module.
include_class_methods # from sub-module ClassMethods, if present include_class_methods M1, M2 # from specified sub-modules include_class_methods do # defined in a block def some_class_method; end end
As usual, sub-modules must be defined before reference.
# File lib/extended_include.rb, line 106 def include_class_methods (*modules, &block) if !block && modules.empty? && const_defined?(:ClassMethods) Extended_Include.include_class_methods self, self::ClassMethods else Extended_Include.include_class_methods self, *modules, &block end end