From: Alexandre Mutel Date: 2009-11-17T08:30:45+09:00 Subject: Re: How to dynamically include a module and update top level? Robert Klemme wrote: > On 16.11.2009 22:01, Alexandre Mutel wrote: > Basically "include" behaves as if the inheritance chain at the time of > inclusion is copied. Thanks for your response. So it seems that every time a module is mixed-in another top-module, i have to reinclude the top-module into its includers? I tried the following "hack" and seems to work, I would be glad to have some feedback... not sure it's a good ruby habit... even if it's working! module ModuleIncluderTracker def includers() @includers end def included(mod) puts "#{self} included in #{mod}" if !(@includers.include? mod) @includers << mod end if (mod.respond_to? :reinclude) mod.reinclude end end def reinclude() @includers.each { |mod| puts "try to reinclude #{self} in #{mod}"; mod.module_eval "include #{self}"; } end end module A @includers = [] extend ModuleIncluderTracker def toto() "toto" end end module B @includers = [] extend ModuleIncluderTracker def self.late_include(mod) module_eval "include #{mod}" end end irb(main):037:0> include B B included in Object => Object irb(main):038:0> B.late_include "A" A included in B try to reinclude B in Object B included in Object => B irb(main):039:0> toto => "toto" With this, it keeps all the includers up-to-date with new module included (although i have not managed any kind of circular references... not sure if it's possible...) -- Posted via http://www.ruby-forum.com/.