Register now and start sharing your code snippets.
-->

Recurse through a Ruby tree

CSS posted 6 months ago by christian

The model:

   1  class Category
   2  ...
   3    def recurse
   4      yield(self)
   5  
   6      children.each do |child|
   7        child.recurse {|sibling| yield sibling}
   8      end
   9    end
  10  end

The recusion:

   1  Category.root.recurse do |child|
   2    puts child
   3  end

Tagged recursive, tree, category, ruby