From: Tachikoma Date: 2008-07-16T17:05:16+09:00 Subject: Re: Is there a way to call include for an object instance? On Jul 16, 12:11 pm, "Eric I." wrote: > Hi Chris, > > > I need to be able to create an object and, based on the param that I > > pass in on the class create_for_sport method, create an object with the > > proper module included. > > You can call extend on an instance to mix in a modules instance > methods to the instance (and leaving the class itself alone).  In your > code, we also need to convert your string to a reference to the module > as well, via a call to const_get.  And this would be your new sport= > method: > >   def sport=(sport) >     extend(self.class.const_get(sport.capitalize + "Stats")) >   end > > I believe that will work for you. > > Eric > > ==== > > LearnRuby.com offers Rails & Ruby HANDS-ON public & ON-SITE workshops. > Please visithttp://LearnRuby.comfor all the details. you should put module(s) into class ,to ensure class Stats has the permission of module,so try following code class Stats module HockeyStats def to_sport puts "hockey" end def scoring_percentage 0.34 end def before_save puts "in the hockey before save" end end module SoftballStats def to_sport puts "softball" end def batting_average 0.563 end def before_save puts "in the softball before save" end end def self.create_by_sport(sport) s = Stats.new s.sport = sport return s end def sport=(sport) eval "extend #{sport.to_s.capitalize}Stats" end end