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?



by
TheR
-- 
Posted via http://www.ruby-forum.com/.