Простой вспомогательный класс и немного рекурсии - это все, что вам нужно:
class Tree
attr_reader :root
def initialize
@root = { :title => 'Home', :path => [ ], :children => [ ] }
end
def add(p)
r_add(@root, p[:key].dup, p[:value])
self
end
private
def r_add(h, path, value)
if(path.empty?)
h[:title] = value
return
end
p = path.shift
c = h[:children].find { |c| c[:path].last == p }
if(!c)
c = { :title => nil, :path => h[:path].dup.push(p), :children => [ ] }
h[:children].push(c)
end
r_add(c, path, value)
end
end
А затем:
t = a.inject(Tree.new) { |t, h| t.add(h) }
h = t.root
даст это в h
:
{:title =>"Home", :path=>[], :children=>[
{:title=>"About", :path=>["about"], :children=>[]},
{:title=>"Services", :path=>["services"], :children=>[
{:title=>"Plans", :path=>["services", "plans"], :children=>[]},
{:title=>"Training", :path=>["services", "training"], :children=>[
{:title=>"Python", :path=>["services", "training", "python"], :children=>[]},
{:title=>"Ruby", :path=>["services", "training", "ruby"], :children=>[]}
]}
]}
]}
Вы можете отсортировать пустые :children
, если они имеют значение.