From: KJF Date: 2007-03-29T21:45:05+09:00 Subject: Re: Inheritance not working as expected On Mar 29, 1:04 pm, "Chris Carter" wrote: > On 3/29/07, KJF wrote: > > > > > I am trying to build up some classes using inheritance but I'm not > > getting the expected results > > > Here are my classes and test data > > > class Control > > attr :attributes, true > > > def initialize(x, y, w, h) > > @attributes = { > > 'x'=>x, > > 'y'=>y, > > 'h'=>w, > > 'w'=>h > > } > > end > > > def getAttributes > > return @attributes > > end > > end > > > class StaticTextControl < Control > > > def initialize(x, y, w, h, xBorder, yBorder, fontSize, fontMode, > > phrase) > > super(x, y, w, h) > > @@desc = 'STATIC_TEXT_CONTROL' > > @attributes = { > > 'xBorder'=>xBorder, > > 'yBorder'=>yBorder, > > 'fontSize'=>fontSize, > > 'fontMode'=>fontMode, > > 'phrase'=>phrase > > } > > end > > > def getAttributes > > return @attributes.merge(super) > > end > > end > > > stc = StaticTextControl.new(1,2,3,4,5,6,7,8,9) > > puts stc.getAttributes > > > When I run the script I get this: > > returning attributes {"phrase"=>9, "xBorder"=>5, "fontSize"=>7, > > "yBorder"=>6, "fontMode"=>8} > > phrase9xBorder5yBorder6fontSize7fontMode8 > > > I am expecting the attributes from the super class to be included as > > well. > > Can anyone tell me what I'm doing wrong? > > > Thanks. > > You call super, which assigns the @attributes hash, then you just > reassign it. In the subclass you need to add to the hash not > overwrite it. > > > super(x, y, w, h) > > @@desc = 'STATIC_TEXT_CONTROL' > > @attributes.merge( { > 'xBorder'=>xBorder, > 'yBorder'=>yBorder, > 'fontSize'=>fontSize, > 'fontMode'=>fontMode, > 'phrase'=>phrase > }) > > Chris Carter > concentrationstudios.com > brynmawrcs.com Thanks for the help guys, makes sense now.