On 09/30/2010 07:48 AM, Regis d'Aubarede wrote:
> def printTree(tree,indent)
>   if (Array === tree)
>       tree.map { |child| printTree(child,indent+"|  ") }
>   else
>       puts(indent.gsub(/\s+$/,"--")+tree.to_s)
>   end
> end
> printTree([1,2,[11,22,[111,222,333,444],33],3,4],"")

IMO this is more idiomatic:

class Object
   def recursive_print indent = ""
     puts indent.gsub(/\s+$/,"--") + to_s
   end
end

module Enumerable
   def recursive_print indent = ""
     map { |child| child.recursive_print indent+"|  " }
   end
end

[1,2,[11,22,[111,222,333,444],33],3,4].recursive_print