2009/4/24 Damjan Rems <d_rems / yahoo.com>: > Robert Klemme wrote: >> On 24.04.2009 05:01, Andrew Timberlake wrote: >>>>> >>>>> I create my_src.rb where I write: >>>>> ¨Ââê åöáìÆéìå®âáóåîáí卿éìåîáíå©®ãáðéôáìéúå §®îå÷§ >>>>> ¨Ââê®äïßóïíåôèéî>>>>> 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? ¨Âïõ óáùï÷áîô ôèôèéòãìáóôï >> subclass either A or B. ¨Âõô ôèãïäå ùïðòåóåîôåêõóô òåðìáãåó ôè>> first occurrence of "Abstract" with something else. ¨Âìóï¬ ùïõò >> 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. ¨Âèå ñõåóôéïî éó÷è>> 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: > > ¨Âåóðïîäßôäï üæïòíáôü > ¨Âïòíáô®èôíìòåîäåò ºæéì½¾ §íùÒåðïòôÆéìå§æïòíáô ½¾ §ÈÔÍ̧ > ¨Âïòíáô®ðäòåîäåò ºæéì½¾ §íùÒåðïòôÆéìå§æïòíáô ½¾ §ÐÄÆ> ¨Âîä > > 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/