Newbie Question

J

james.d.masters

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

Dick K.

What I am really looking for is the concept of lvalue's. There are many
other situations where they come in handy. Another contrived example
might be:

a = \array[123]['abc']['def']
if (some_condition)
return a
a = some_value

then you don't compute the index for array twice.

Anything like that? (I have not been able to find it...)

P.S. The \ is how you would do it in perl.

Chris said:
@@dcounts[difficulty][artistid]
test.rb:14: parse error, unexpected '=', expecting $
Song.count(1,3547) = 10
^

You could do something like:
def Song.count=(*args)
@@dcounts[args[0]] ||= {}
@@dcounts[args[0]][args[1]] = args[2]
end

Song.count 1, 3547, 10
 
G

Gary Wright

What I am really looking for is the concept of lvalue's. There are
many
other situations where they come in handy. Another contrived example
might be:

a = \array[123]['abc']['def']
if (some_condition)
return a
a = some_value

then you don't compute the index for array twice.

There is nothing like that in Ruby. You can avoid some of
the extra lookups in your example:

container = array[123]['abc']
if (come_condition)
return container['def']
else
container['def'] = some_value

The example seems a bit contrived. I think with some more
context a better solution might be found.

Gary Wright
 

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

Similar Threads


Members online

Forum statistics

Threads
474,234
Messages
2,571,178
Members
47,809
Latest member
Adisty

Latest Threads

Top