我在使用 ruby 从物化路径构建树结构时遇到了麻烦。
假设我有一个排序的结果集(来自 couchdb):
[
{ :key => [], :value => "Home" },
{ :key => ["about"], :value => "About" },
{ :key => ["services"], :value => "Services" },
{ :key => ["services", "plans"], :value => "Plans" },
{ :key => ["services", "training"], :value => "Training" },
{ :key => ["services", "training", "python"], :value => "Python" },
{ :key => ["services", "training", "ruby"], :value => "Ruby" }
]
我只需要它作为红宝石中的一棵树,以下哈希就足够了:
{ :title => "Home", :path => [], :children => [
{ :title => "About", :path => ["about"] },
{ :title => "Services", :path => ["services"], :children => [
{ :title => "Plans", :path => ["services", "plans"] }
]}
]}
有人可以帮我吗?