Re: Is there a better way of doing this



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/

.



Relevant Pages

  • Re: Is there a better way of doing this
    ... Robert Klemme wrote: ... What I am trying to do is create a reporting sistem, ... the future which should expand reporting system to XX format when ... But it looks like ugly hack and I don't consider myself to be Ruby ...
    (comp.lang.ruby)
  • Re: crystal report print to file
    ... it can read from any ODBC/JDBC source ... It writes files to a standard XML format not some proprietary format. ... an extra dev tool or external reporting system as any kind of advantage. ... what MS licensing issues do you perceive to exist regarding CR? ...
    (microsoft.public.vb.crystal)
  • Re: does rubys strftime not attempt POSIX-compliance?
    ... it refers to the "POSIX locale concept" and the ruby compliance with it. ... MS> letters) but you have only one locale. ... this is ruby's strftime that operates on ruby Time values and has ... I started using this ISO date format next to my signature on forms back in the early eighties. ...
    (comp.lang.ruby)
  • Ruby IPC In an OpenMosix Cluster
    ... format, transform it into one of _many_ other formats, compile and send ... and Ruby for the application itself. ... MTA): receive, transform, compile/send. ... What is the best form of IPC ...
    (comp.lang.ruby)
  • Re: unpack signed short in network (big-endian) byte order
    ... One interesting wrinkle is that where ruby uses the ... but integer length and byte-order are defined by a modifier at the ... introduce more cryptic format specifiers. ...
    (comp.lang.ruby)