How to handle recursive parent/child problems like this?

Posted by lsdude on Stack Overflow See other posts from Stack Overflow or by lsdude
Published on 2010-03-08T16:55:08Z Indexed on 2010/03/08 17:36 UTC
Read the original article Hit count: 186

Filed under:
|
|

In web dev I come across these problems a lot.

For example, we have a giant list of URLs that are in this format:

/businesses  
/businesses/food  
/businesses/food/wendys  
/businesses/food/wendys/chili  
/businesses/food/wendys/fries  
/businesses/food/wendys/chicken-nuggets  
/businesses/pharmacy/cvs  
/businesses/pharmacy/cvs/toothpaste  
/businesses/pharmacy/cvs/toothpaste/brand  
...

and then we need to output each one, where the parent category is in h1 tags, the child is in h2 tags, and the children of that are in h3 tags.

I can handle this but I feel my code is messy. I'm sure there is a design pattern I can use? Langs are ruby/php usually. how would you handle this?

© Stack Overflow or respective owner

Related posts about recursion

Related posts about ruby