From: Dustin Barker Date: 2008-06-11T03:14:07+09:00 Subject: Re: Basic Tree Data Structure Hi Justin, The second 3 is the second iteration of the parent's child_nodes.each - try replacing: child_node.each { |e| puts "3"; yield e; } with this: child_node.each { |e| puts "3 #{value}"; yield e; } to see which iteration belongs to which node. -Dustin On Jun 10, 2008, at 1:46 PM, Justin To wrote: > class Tree > attr_reader :value > def initialize(value) > @value = value > @children = [] > end > > def <<(value) > subtree = Tree.new(value) > @children << subtree > return subtree > end > > end > > t = Tree.new("Parent") > child1 = t << "Child 1" > child2 = t << "Child 2" > gc1 = child1 << "GC 1.1" > > class Tree > def each > puts "1" > yield value > puts "2" > @children.each do |child_node| > puts "in child_node" > child_node.each { |e| puts "3"; yield e; } > end > end > end > > t.each { |x| puts "t.each"; puts x } > > > OUTPUT: > > 1 > t.each > Parent > 2 > in child_node > 1 > 3 > t.each > Child 1 > 2 > in child_node > 1 > 3 # why are there two 3's here?? > 3 # ????? Doesn't Child 1 only have one child in its array > @children?? > t.each > GC 1.1 > 2 > in child_node > 1 > 3 > t.each > Child 2 > 2 > > > (^ comment) > > > Thanks in advance > -- > Posted via http://www.ruby-forum.com/. >