# Inheritance not working as expected

**URL:** <https://www.ruby-forum.com/t/inheritance-not-working-as-expected/90516>\
**Category:** Ruby\
**Created:** [March 29, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/inheritance-not-working-as-expected/90516 "2007-03-29T13:40:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KJF](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@KJF](https://www.ruby-forum.com/u/KJF)\
**Post date:** [March 29, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/inheritance-not-working-as-expected/90516/1 "2007-03-29T13:40:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KJF](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@KJF](https://www.ruby-forum.com/u/KJF)\
**Post date:** [March 29, 2007, 2:05pm UTC](https://www.ruby-forum.com/t/inheritance-not-working-as-expected/90516/2 "2007-03-29T14:05:42Z")

</div>

On 3/29/07, KJF [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 'x'=>x,
> 
> 'fontMode'=>fontMode,
> 
> ```
> 
> puts stc.getAttributes
> 
> 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 C.  
[concentrationstudios.com](http://concentrationstudios.com)  
[brynmawrcs.com](http://brynmawrcs.com)

---

<div class="post-metadata">

**Author:** ![KJF](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@KJF](https://www.ruby-forum.com/u/KJF)\
**Post date:** [March 29, 2007, 2:06pm UTC](https://www.ruby-forum.com/t/inheritance-not-working-as-expected/90516/3 "2007-03-29T14:06:42Z")

</div>

On Mar 29, 7:35 am, “KJF” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
…

> ```
> 'w'=>h
> 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  
> }  
> …  
> @attributes is an instance attribute, so right here you change it,  
> loosing the x,y,w,h values.

you should use merge here, and then there is no need to re-implement  
getAttributes as the superclass method will work as expected

Cheers  
Chris

---

<div class="post-metadata">

**Author:** ![KJF](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@KJF](https://www.ruby-forum.com/u/KJF)\
**Post date:** [April 1, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/inheritance-not-working-as-expected/90516/4 "2007-04-01T20:21:18Z")

</div>

> Can anyone tell me what I’m doing wrong?
> 
> Thanks.

Besides what others have pointed out, getAttributes is member function  
that should not be used.  
This is a bad practice, likely carried over from more limiting  
languages such as Java, C++ or Python.

You should use:

class Control  
attr\_reader :attributes

```
    def initialize(x, y, w, h)
            @attributes = {
                    'x'=>x,
                    'y'=>y,
                    'h'=>w,
                    'w'=>h
            }
    end

```

end

a = Control.new  
attrs = a.attributes

If you do require attributes() to do something special in this or  
derived class, you can just redefine it.

class DerivedControl \< Control  
def attributes  
nil  
end  
end

The benefit of this approach is that your interface never changes,  
regardless of whether the function is a getter/setter or a true  
function that performs some more complex magic.

If, on the other hand, you do require a special member function for a  
different operation other than a getter/setter, you should stick to  
the ruby convention of lowercase and underscores. Thus, instead of  
calculateAttributes(), you’d use calculate\_attributes().

---

<div class="post-metadata">

**Author:** ![KJF](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/59ef9b/32.png) [@KJF](https://www.ruby-forum.com/u/KJF)\
**Post date:** [March 29, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/inheritance-not-working-as-expected/90516/5 "2007-03-29T14:46:06Z")

</div>

On Mar 29, 1:04 pm, “Chris C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > attr :attributes, true
> > def getAttributes
> > @attributes = {
> > end
> > 
> > ```
> > 
> > I am expecting the attributes from the super class to be included as  
> > @@desc = ‘STATIC\_TEXT\_CONTROL’  
> > [concentrationstudios.com](http://concentrationstudios.com)  
> > [brynmawrcs.com](http://brynmawrcs.com)

Thanks for the help guys, makes sense now.
