Re: Is there a better way of doing this
- From: Robert Klemme <shortcutter@xxxxxxxxxxxxxx>
- Date: Fri, 24 Apr 2009 02:41:24 -0500
2009/4/24 Damjan Rems <d_rems@xxxxxxxxx>:
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?
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?
Yes. This is a textbook example for using delegation over
inheritance. You should make your Report class *use* a format instead
of *inheriting* a format. You can still have a formatter base class
which implements common things (e.g. if you want your numbers to come
out identical in all reports then you could have a number formatting
method there). But the different formatting (e.g. using <li> vs. some
PDF markup) is done in the specific formatter class.
It seems to me that inheritance is generally overused and people
should more often resort to delegation. Maybe we should blog about
this - although it is not generally a Ruby best practice to delegate
instead of inherit.
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
.
- Follow-Ups:
- Re: Is there a better way of doing this
- From: Damjan Rems
- Re: Is there a better way of doing this
- References:
- Is there a better way of doing this
- From: Damjan Rems
- Re: Is there a better way of doing this
- From: Tim Hunter
- Re: Is there a better way of doing this
- From: Andrew Timberlake
- Re: Is there a better way of doing this
- From: Robert Klemme
- Re: Is there a better way of doing this
- From: Damjan Rems
- Is there a better way of doing this
- Prev by Date: Re: Is there a better way of doing this
- Next by Date: Re: reformatting a text file that has some binary in it
- Previous by thread: Re: Is there a better way of doing this
- Next by thread: Re: Is there a better way of doing this
- Index(es):
Relevant Pages
|