Robert Klemme wrote:
> On 24.04.2009 05:01, Andrew Timberlake wrote:
>>>>
>>>> I create my_src.rb where I write:
>>>>  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?
> 
> The question is: of doing what?  You say you want the third class to
> subclass either A or B.  But the code you presented just replaces the
> first occurrence of "Abstract" with something else.  Also, your
> run_class is not called with the second parameter.
> 
> If you want to control inheritance from either A or B you can do
> 
> class X < condition ? A : B
> end
> 
> But frankly, this approach seems strange at least.  The question is, why
> do you need make the inheritance structure of your class configurable?
> The suggestion to make A and B mixin modules seems much better to me.
> You can even extend instances of your class with a module on a case by
> case basis.
> 
> Can you present a bigger picture so we can understand what motivates
> your approach?
> 
> Kind regards
> 
>   robert

What I am trying to do is create a reporting sistem, something like 
Ruport, but simplier and different. So my Class A is ReportPDF and Class 
B is ReportHTML and Abstract is ReportBase which defines methods and 
variables common to all subclasses. And there can be ReportXX Class in 
the future which should expand reporting system to XX format when 
required.

What I have accomplished so far is I can call:

run_report('myReportFile', :format => 'HTML')
run_report('myReportFile', :format => 'PDF')

and get result in pdf or html file, with no changes to myReportFile.rb. 
Final idea is to make it Rails plugin and run it maybe like this:

  respond_to do |format|
    format.htmlp { render :file => 'myReportFile', format => 'HTML' }
    format.pdf   { render :file => 'myReportFile', format => 'PDF' }
  end

I guess I still have to learn a lot until then.

But it looks like ugly hack and I don't consider myself to be Ruby 
hacker. I am just using what I have learned so far and mix it with my 
previuos experiances. Which is mostly wrong because I am always thinking 
on how I used to do this before Ruby.

So I am asking if this can be accomplished differently?


by
TheR






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