Damjan Rems wrote:
> I have three classes.
> module mymodule
> 
> class Abstract
> end
> 
> class A < Abstract
> end
> 
> class B  < Abstract
> end
> 
> end
> 
> And I want the third class to subclass class A or B dependent on
> parameter passed. my_src is also the name of source file:
> 
> This is what I do:
> 
> I create my_src.rb where I write:
> class my_src < Abstract
> ....
> end
> 
> Finally I have a method to load my class and do something with it.
> def run_class(filename, parm)
>   src = File.read(filename + '.rb')
>   src.sub!('Abstract', parm)
> # Evaluate is equal to load
>   eval src.to_s
>   obj = eval( File.basename(filename).capitalize + '.new' )
>   obj.do_something
> 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.

-- 
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407