From: Aaron Patterson Date: 2009-09-13T07:41:18+09:00 Subject: Re: choosing a module name at runtime On Sep 12, 2009, at 3:26 PM, John Smirnoff wrote: > My application chooses at runtime the input format of some data, for > example :xml or :mysql. Then, the app instantiates a DataManager. The > DataManager class should 'include' the appropriate Module (either > DataIOXML or DataIOMySQL). > I tried to use include in the initialize method of DataManager, but > that > doesn't work. > Can I include a module in the initialize method of the class? If yes, > how? > If no: Any other ways how I can choose the correct module to include > at > runtime? You want the "extend" method, not the "include" method: module A def foo "A: foo" end end module B def foo "B: foo" end end class C def initialize which extend(which == :a ? A : B) end def metaclass; class << self; self end end end p C.new(:a).metaclass.ancestors p C.new(:b).metaclass.ancestors Hope that helps. --- Aaron Patterson http://tenderlovemaking.com