On Mar 2, 10:57 am, “Rick DeNatale” [email protected] wrote:
It seems like this is approaching the notions of delegation rather
than inheritance … And these named slots were resolved by walking
up a chain of objects (rather than having a superclass you delegate
to another object) …
Thanks Rick and all who have added their thoughts into this
discussion. The more I think about it I can see that the behavior
that I was looking for would likely not be used as often as I first
thought. Here is a simplified example of what brought me to this
question in the first place. I have a CAD-related script that uses
quite a bit of inheritence. I had reference points as coordinates
that could be defined at a parent class or inherited class level; yet
not cross namespace with siblings. I use classes instead of instances
to store this data because multiple objects are created from these
classes and all objects would use the same reference points of the
class. Here is an example (using Brian’s trick from his earlier
post):
class Canvas
class << self
def ref_pt(name, value=nil)
@pts = {} if @pts.nil?
if value.nil?
# get value
self.ancestors.each { |k|
k.instance_eval { return @pts[name] if not @pts.nil? and
@pts.has_key? name }
}
nil
else
# set value
@pts[name] = value
end
end
end
end
class Canvas1 < Canvas
end
class Canvas1A < Canvas1
end
class Canvas2 < Canvas
end
Set some reference points as an example
Canvas.ref_pt(:common_pt, [1.23, 2.34])
Canvas1.ref_pt(:sub1_pt, [3.45, 4.56])
Canvas1A.ref_pt(:sub1a_pt, [5.67, 6.78])
Canvas2.ref_pt(:sub2_pt, [7.89, 8.90])
shortcut array to speed up output
klasses = [Canvas, Canvas1, Canvas1A, Canvas2]
Show points common to descendents of Canvas
puts “Variable set at Canvas level (:common_pt):”
klasses.each {|k| puts “#{k}: #{k.ref_pt(:common_pt).inspect}”}
puts ‘-’ * 78
Show points common to descendents of Canvas1
puts “Variables set at Canvas1 level (:sub1_pt)”
klasses.each {|k| puts “#{k}: #{k.ref_pt(:sub1_pt).inspect}”}
puts ‘-’ * 78
Show points common to descendents of Canvas1A
puts “Variables set at Canvas1A level (:sub1a_pt)”
klasses.each {|k| puts “#{k}: #{k.ref_pt(:sub1a_pt).inspect}”}
puts ‘-’ * 78
Show points common to descendents of Canvas2
puts “Variables set at Canvas2 level (:sub2_pt)”
klasses.each {|k| puts “#{k}: #{k.ref_pt(:sub2_pt).inspect}”}
And the output for convenience:
Variable set at Canvas level (:common_pt):
Canvas: [1.23, 2.34]
Canvas1: [1.23, 2.34]
Canvas1A: [1.23, 2.34]
Canvas2: [1.23, 2.34]
Variables set at Canvas1 level (:sub1_pt)
Canvas: nil
Canvas1: [3.45, 4.56]
Canvas1A: [3.45, 4.56]
Canvas2: nil
Variables set at Canvas1A level (:sub1a_pt)
Canvas: nil
Canvas1: nil
Canvas1A: [5.67, 6.78]
Canvas2: nil
Variables set at Canvas2 level (:sub2_pt)
Canvas: nil
Canvas1: nil
Canvas1A: nil
Canvas2: [7.89, 8.9]
If this adds more confusion, then please disregard :). Otherwise,
thanks again for the input from everyone…