Well actually you can ... OP updated code, so here is my implementation:
class Module def submodules constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == Module} end end module C # this gets called when the module extends another class or module # luckily it does _not_ get called when we extend via :send def self.extended(base) # collect all submodules and extend them with self base.submodules.each{|m| m.send :extend, self } end def c1 puts "c1" end end module Top module A;end module B;end # extend needs to go at the end - otherwise Top doesnt know about its submodules extend C end Top.c1 # => "c1" Top::A.c1 # => "c1" Top::B.c1 # => "c1"
source share