From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2002-10-30T05:35:30+09:00 Subject: Re: problem with recursive structure In article <5.1.0.14.2.20021029141528.00b146e8@zcard04k.ca.nortel.com>, Mark Probert wrote: > >Dear Rubists, > >I have a little class called SimpleTree that makes a recursive >data structure of arrays. I am getting some funny behaviour when >I traverse the tree for printing. > >The print routine is: > > def pp_branch(b) > puts "(head) #{b[0]}" > b.each { |item| > next if item == b[0] # avoid the head > pp_branch(item) if item.type == Array > puts "(leaf) #{item}" > } > end > >Output looks like: > >["a", "b", ["z/", "a", "b", ["y/", "c", "d"], "e"]] >(leaf) a >(leaf) b >(head) z/ >(leaf) a >(leaf) b >(head) y/ >(leaf) c >(leaf) d >(leaf) y/cd >(leaf) e > >It is the line "y/cd" that gets me. Ruby (or is it me?) appears >to be flattening included array "y". > >Anyone help me with this? >TIA, > I don't really have an answer for you, but I get something slightly different(but still wrong) when I run this in 1.6.7: (head) a (leaf) b (head) z/ (leaf) a (leaf) b (head) y/ (leaf) c (leaf) d (leaf) y/cd (leaf) e (leaf) z/aby/cde <------ note the last line is in addition to what you had. I ran this in the debugger and it seems that at the end of a sub-array it prints the while sub-array as a leaf. I couldn't figure out why it was doing this. What version of Ruby are you running? Phil