Topic: Anonymous Recursive Lambda & me

I'm using the ancestry gem for a project, and the ancestry gem can arrange the tree into a hash of objects (in my case, categories), and it looks like: http://pastie.org/1233134

But I need to rearrange the hash for a form select box, i.e.: we only need the name and the id. So how can I do this recursively? I imagine you use inject in some fancy fashion, which I can't grasp for this, since it needs to recursively dig deeper into the hash. The best I could do is making a new array and grabbing the data after looping over each piece: something like:

    x = lambda { |k|
      array << [("-" * k.depth.to_i)+ " " + k.name, k.id]
      if k.has_children?
        k.children.map(&x)
      end
    }

Is there a better way to do this with inject?

Last edited by scott32 (2010-10-19 13:04:34)