Inheritance not working as expected

K

KJF

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

ChrisH

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
 
C

Chris Carter

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
 
K

KJF

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.


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

gga

Can anyone tell me what I'm doing wrong?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top