Inheritance not working as expected

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.

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

···

On 3/29/07, KJF <Kevin.Fagan1@gmail.com> wrote:

...

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
                }

...
@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

···

On Mar 29, 7:35 am, "KJF" <Kevin.Fag...@gmail.com> wrote:

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().

Thanks for the help guys, makes sense now.

···

On Mar 29, 1:04 pm, "Chris Carter" <cdcar...@gmail.com> wrote:

On 3/29/07, KJF <Kevin.Fag...@gmail.com> 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