From: Robert Klemme Date: 2009-04-24T20:13:38+09:00 Subject: Re: Is there a better way of doing this 2009/4/24 Damjan Rems : > Joel VanderWerf wrote: >> Damjan Rems wrote: >>> end >>> .... >>> end >>> run_class('my_src') >>> >>> Ruby is wonderful language which allows us to do this. But the >>> substitution part is kinda ugly to me. >>> >>> So is there a better way of doing this? >> >> One idea: define A and B as modules instead of classes. Then, when you >> load the file that defines "class MySrc < Abstract", you don't have to >> substitute anything. Just include the module, as in: >> >>    Module ModA; ...; end >> >>    load filename + ".rb" >>    cl = Object.const_get( File.basename(filename).capitalize ) >>    cl.class_eval {include ModA} > >> >> Now, instances of cl will have all the methods of module ModA. > > I am trying this variation for now. And it might look like good until > here: >>    cl.class_eval {include ModA} > > I guess my version is wrong >    cl.class_eval { "include 'DrgReports#{opts[:format]}'" } Here you are creating a string only. >    report = eval( File.basename(custom_class).capitalize + '.new' ) > > because after this I get error that method found in included module is > missing. > > > How to write the line correct? This should work: cl.class_eval "include 'DrgReports#{opts[:format]}'" But again, inclusion - which is a special form of inheritance - does not seem the proper technique here. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/