From: Rick DeNatale Date: 2010-02-01T23:13:52+09:00 Subject: Re: Inheritance related problem Just to be clear I meant to say why not just factor out JUST what's different. On Mon, Feb 1, 2010 at 8:59 AM, Rick DeNatale wrote: > On Mon, Feb 1, 2010 at 1:54 AM, R. Kumar wrote: >> Sean DeNigris wrote: >>>> When Parent's printa is called, it calls its own printb. >>>> However, when Child's printa is called, it calls Parent's printa, which >>>> (in this case) i was hoping would call Parent's printb directly. But it >>>> (correctly) calls Child's printa which once again reduces the arg. >>>> >>>> So my arg gets reduced twice. I suppose i could use some flags to >>>> prevent this, But is there any direct way i can coerce Parent printa to >>>> call only Parent printb. >>> >>> I'm having trouble understanding how you got into this position.  I'm >>> assuming that you don't own Parent, and so have to work your magic in >>> Child.  For the specific example you described, the following will >>> work... >>> >> >> I own both. Parent is a wrapper around ncurses Window. Child is a >> wrapper around ncurses Pad. However, they are used in a similar manner. >> So in the original app, i used only Window. Now in many cases I use Pad. >> They both should work interchangeably since they essentially do the same >> thing but often in different ways. An object could use Window, or Pad. >> >> However, stuff like printing a border or writing a string onto the >> window are essentially the same except that the offsets in Window are >> absolute whereas the Pad offsets are relative. So when Pad calls super >> it only has to make the relative offsets absolute: >> >>    def printstring(row, col, value, color, attrib=Ncurses::A_NORMAL) >>      super(row - @top, col - @left, value, color, attrib) >>    end # printstring >> >>    def print_border row, col, height, width, color, >> att=Ncurses::A_NORMAL >>      super(row - @top, col - @left, height, width,  color, att) >>    end >> >> Now in the parent Window, print_border does a call to printstring to >> reset the background. Unfortunately, the child's printstring gets called >> and @top and @left get decremented again. > > So why not just factor out what's different between Parent and Child > > class Parent >  def printstring(row, col, ... other parameters omitted) >      row, col = *transform(row, col) >      # do whatever you need >  end > >  def transform(row, col) >     [row, col] >  end > end > > class Child < Parent >   def transform(row, col) >       [row - @top, col - @left] >   end > end > > -- > Rick DeNatale > > Blog: http://talklikeaduck.denhaven2.com/ > Twitter: http://twitter.com/RickDeNatale > WWR: http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn: http://www.linkedin.com/in/rickdenatale > > -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale