Mark Probert (probertm / nortelnetworks.com) wrote:
> 
> 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
> 

When you use string interpolation, to_s is called implicitly.  So in the
above, "(leaf) #{item}" does "(leaf) #{item.to_s}" on Arrays.  The to_s
of an Array flattens and concatenates the elements of an Array:

  [ "y/", "c", "d" ].to_s
  #=> "y/cd"

Try:

  def pp_branch(b)
      puts "(head) #{b[0]}"
      b[1..-1].each { |item|
          if Array === item
            pp_branch(item) 
          else
            puts "(leaf) #{item}"
          end
      }
  end

_why