I have a 'hash tree' like this:
dat = {
'building' => {'street' => 'High Street 10', 'people' =>[Person, Person]}
}
Person can be whatever.
So, in my co开发者_Python百科de, I directly access 'building' like so: dat['building']
, now is there a way to somehow go, up the tree, to have access to {'building' => {...}}
, again?
I currently have a function that let's me access either of those:
def info with_root = false
with_root ? dat : dat['building']
end #info
It doesn't look criminally bad (I think), but I hope there is a nicer way.
Thanks!
Short answer: no.
The reason is this.
Suppose you have:
addr = {'street' => 'High Street 10', 'people' =>[Person, Person]}
dat1 = {'building' => addr}
dat2 = {'zip'=>55117, 'electricBillID'=>11223344, 'address' => addr
What happens when you go "up"? Do you get to dat1 or dat2?
If you really need to be able to do this -- and it's not obvious that you do -- you'll need to use a more complex data structure that includes links to the containing objects -- something like a tree (https://rubygems.org/gems/rubytree) or a simple graph structure.
And, as has been mentioned, if you just hang onto the root object, you can always crawl your way back down the hash.
精彩评论