J
Juston Davies
I'm building a large system that needs to have a data structure of
things that look and act similar to one another in a kind of
composition tree. Take a look at my small scale test code below:
class Main
def main
#instantiate objects
three = Child3.new
two = Child2.new
one = Child1.new
#build tree
two.addNode three
one.addNode two
#execute tree
one.execute "Who is "
end
end
class Parent
#Has some composing nodes
@nodes
#Init code
def initialize
@nodes = Array.new
end
#add a node to object
def addNode node
@nodes << node
end
#Generalized execute code
def execute str
puts str + self.to_s
@nodes.each {|node| node.execute str}
end
end
class Child1 < Parent
#The same a parent in this scenerio but has other functionality not
illustrated here
end
class Child2 < Parent
#The same a parent in most cases, needs to do "extra" in execute
calls
def execute str
puts str + self.to_s
#@nodes.each {|node| node.execute "Batman?"} <---This works but its
really ideal for code recycling
#str = "Robin" <---This actually causes subsequent levels to print
Robin instead of Batman? or Who is
super.execute "Batman?"
end
end
class Child3 < Parent
#The same a parent in this scenerio but has other functionality not
illustrated here
end
#Just so I can execute my Test.rb easy in eclipse, not really
important
if __FILE__ == $0
Main.new.main
end
When I designed the code I expected what would print out would be:
The first problem I see is that when I call super.execute it seems to
want to ignore my argument and favor of the argument I gave at the
hight level of scope. (If I do str = "Robin" in Child2's execute but
still call super.execute("batman") it prints ">Robin" in subsequent
levels.) The second is that Child2 somehow calls execute on itself
once before digging into the next level of the tree, then at the last
level it tries to call it on an empty array which goes against the
program logic. I'm not new to programming, I've probably built this
exact same thing in Java, but I am new to Ruby. Whats wrong with what
I'm trying to do?
Thanks in advance!
things that look and act similar to one another in a kind of
composition tree. Take a look at my small scale test code below:
class Main
def main
#instantiate objects
three = Child3.new
two = Child2.new
one = Child1.new
#build tree
two.addNode three
one.addNode two
#execute tree
one.execute "Who is "
end
end
class Parent
#Has some composing nodes
@nodes
#Init code
def initialize
@nodes = Array.new
end
#add a node to object
def addNode node
@nodes << node
end
#Generalized execute code
def execute str
puts str + self.to_s
@nodes.each {|node| node.execute str}
end
end
class Child1 < Parent
#The same a parent in this scenerio but has other functionality not
illustrated here
end
class Child2 < Parent
#The same a parent in most cases, needs to do "extra" in execute
calls
def execute str
puts str + self.to_s
#@nodes.each {|node| node.execute "Batman?"} <---This works but its
really ideal for code recycling
#str = "Robin" <---This actually causes subsequent levels to print
Robin instead of Batman? or Who is
super.execute "Batman?"
end
end
class Child3 < Parent
#The same a parent in this scenerio but has other functionality not
illustrated here
end
#Just so I can execute my Test.rb easy in eclipse, not really
important
if __FILE__ == $0
Main.new.main
end
When I designed the code I expected what would print out would be:
Who is <Child1>
Who is <Child2>
Batman? <Child3> But I get:
Who is <Child1>
Who is <Child2>
Who is <Child2>
Who is <Child3>
Error
The first problem I see is that when I call super.execute it seems to
want to ignore my argument and favor of the argument I gave at the
hight level of scope. (If I do str = "Robin" in Child2's execute but
still call super.execute("batman") it prints ">Robin" in subsequent
levels.) The second is that Child2 somehow calls execute on itself
once before digging into the next level of the tree, then at the last
level it tries to call it on an empty array which goes against the
program logic. I'm not new to programming, I've probably built this
exact same thing in Java, but I am new to Ruby. Whats wrong with what
I'm trying to do?
Thanks in advance!