Skip to content

Instantly share code, notes, and snippets.

/data Secret

Created October 10, 2012 18:57
Show Gist options
  • Save anonymous/a6f6a57c3dc47d365e38 to your computer and use it in GitHub Desktop.
Save anonymous/a6f6a57c3dc47d365e38 to your computer and use it in GitHub Desktop.
@data={ "p4" => {
'parent_id' => "p1"
'id' => "p10"
},
"p2" => {
'parent_id' =>"p1"
'id'=>"p20"
},
"p11" =>{
'parent_id'=>"p20"
'id' =>"p111"
},
"p22" =>{
'parent_id'=>"p2"
'id'=>"p123"
}
}
def child_nodes(root_node="p1")
@data.select { |k,v| v.parent_id == root_node }
end
def flatten_childen(id,node_depth=1)
if node_depth == 0
#return something?
else
#decrease depth??
child_nodes(id).merge(child_nodes(id).map { |a,b| b.id }.each { |a| child_nodes(a) })
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment