我需要建立这棵树:
result = [
['t9'],
['t3',
['t4'],
['t8',
['t6'],
['t1',
['t5']
]
]
],
['t7',
['t2']
]
]
从这些对象:
{:id => 't1', :tg => 't8', :rank => 2}
{:id => 't2', :tg => 't7', :rank => 1}
{:id => 't3', :tg => nil, :rank => 2}
{:id => 't4', :tg => 't3', :rank => 1}
{:id => 't5', :tg => 't1', :rank => 1}
{:id => 't6', :tg => 't8', :rank => 1}
{:id => 't7', :tg => nil, :rank => 3}
{:id => 't8', :tg => 't3', :rank => 2}
{:id => 't9', :tg => nil, :rank => 1}
tg
是自引用关联。
rank
是数组中的位置/索引
任何想法(在红宝石中首选)?