# Need: hierarchic data structure

**URL:** <https://www.ruby-forum.com/t/need-hierarchic-data-structure/71437>\
**Category:** Ruby\
**Created:** [February 21, 2006, 6:35pm UTC](https://www.ruby-forum.com/t/need-hierarchic-data-structure/71437 "2006-02-21T18:35:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![henon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/0ea827/32.png) [@henon](https://www.ruby-forum.com/u/henon)\
**Post date:** [February 21, 2006, 6:35pm UTC](https://www.ruby-forum.com/t/need-hierarchic-data-structure/71437/1 "2006-02-21T18:35:53Z")

</div>

hi,  
i am searching for an implementation of a hierarchic datastructure that  
behaves similar to a file system except that get of an inexistent path  
returns nil and set of an unexistent path creates the required nodes  
silently.

example:  
d=HierarchicData.new

d[:foo, :bar] # =\> nil  
d[:foo, :bar]=42  
d[:foo, :bar] # =\> 42

d[:foo, :foo, 0, 1, :bar]=Object.new # creates a hash in an array in an  
array in a hash in a hash

i want to check if this kind of data structure (or similar) has been  
implemented already as library. if not i am going to do it.

thx for any comments!  
– henon

---

<div class="post-metadata">

**Author:** ![henon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/0ea827/32.png) [@henon](https://www.ruby-forum.com/u/henon)\
**Post date:** [February 21, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/need-hierarchic-data-structure/71437/2 "2006-02-21T18:48:01Z")

</div>

On Feb 21, 2006, at 12:33 PM, henon wrote:

> d[:foo, :bar] # =\> nil  
> thx for any comments!  
> – henon

Have you considered this?

class HierarchicData \< Hash  
def   
super(args)  
end

def []=(\*args)  
keys = args[0…(args.length - 2)]  
value = args.last  
super(keys, value)  
end  
end

irb(main):012:0\> d = HierarchicData.new  
=\> {}  
irb(main):013:0\> d[:foo, :bar]  
=\> nil  
irb(main):014:0\> d[:foo, :bar] = 42  
=\> 42  
irb(main):015:0\> d[:foo, :bar]  
=\> 42  
irb(main):016:0\> d[:foo, :foo, 0, 1, :bar] = Object.new  
=\> #Object:0x315d24

---

<div class="post-metadata">

**Author:** ![henon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/0ea827/32.png) [@henon](https://www.ruby-forum.com/u/henon)\
**Post date:** [February 21, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/need-hierarchic-data-structure/71437/3 "2006-02-21T21:25:27Z")

</div>

> There is the usual idiom of nested hashes:

sure there’s no problem implementing it … I guess it has been  
implemented a thousand times by a thousand different people. i am  
asking if anyone knows a library because it might feature iterators,  
clever error handling code, etc. and it might be tested better than yet  
annother quicky-hacky-implementation.

are there any libraries or prominent applications known?  
thanks for response,  
– henon

---

<div class="post-metadata">

**Author:** ![henon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/0ea827/32.png) [@henon](https://www.ruby-forum.com/u/henon)\
**Post date:** [February 21, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/need-hierarchic-data-structure/71437/4 "2006-02-21T19:06:47Z")

</div>

“henon” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) schrieb im Newsbeitrag  
news:removed\_email\_address@domain.invalid…

> d[:foo, :bar]=42  
> d[:foo, :bar] # =\> 42
> 
> d[:foo, :foo, 0, 1, :bar]=Object.new # creates a hash in an array in an  
> array in a hash in a hash
> 
> i want to check if this kind of data structure (or similar) has been  
> implemented already as library. if not i am going to do it.
> 
> thx for any comments!

There is the usual idiom of nested hashes:

insert = lambda {|h,k| h[k] = Hash.new(&insert)}  
tree = Hash.new(&insert)

Then you can do

> > tree[:foo][:bar]  
> > =\> {}  
> > tree[:foo][:bar]=42  
> > =\> 42  
> > tree  
> > =\> {:foo=\>{:bar=\>42}}

Of course you can wrap that in a single class:

class HT  
def initialize  
insert = lambda {|h,k| h[k] = Hash.new(&insert)}  
@tree = Hash.new(&insert)  
end

def   
a.inject(@tree) {|h,k| h[k]}  
end

def []=(\*a)  
val = a.pop  
key = a.pop  
a.inject(@tree) {|h,k| h[k]}[key]=val  
val  
end  
end

> > h[:foo, :bar]  
> > =\> {}  
> > h  
> > =\> #\<HT:0x101dc690 @tree={:foo=\>{:bar=\>{}}}\>  
> > h[:foo, :bar]=24  
> > =\> 24  
> > h  
> > =\> #\<HT:0x101dc690 @tree={:foo=\>{:bar=\>24}}\>  
> > h[:bar]=2345  
> > =\> 2345  
> > h  
> > =\> #\<HT:0x101dc690 @tree={:bar=\>2345, :foo=\>{:bar=\>24}}\>

HTH

```
robert

```

---

<div class="post-metadata">

**Author:** ![henon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/0ea827/32.png) [@henon](https://www.ruby-forum.com/u/henon)\
**Post date:** [February 22, 2006, 1:54pm UTC](https://www.ruby-forum.com/t/need-hierarchic-data-structure/71437/5 "2006-02-22T13:54:34Z")

</div>

henon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > There is the usual idiom of nested hashes:
> 
> sure there’s no problem implementing it … I guess it has been  
> implemented a thousand times by a thousand different people. i am  
> asking if anyone knows a library because it might feature iterators,  
> clever error handling code, etc. and it might be tested better than  
> yet annother quicky-hacky-implementation.
> 
> are there any libraries or prominent applications known?  
> thanks for response,

The basic idiom is so simple (two lines, see my last posting) that it  
doesn’t make any sense to package that into a lib. I don’t know whether  
your requirements are met by anthing done already. Did you check the  
RAA?  
That’s usually a good starting point.

[http://raa.ruby-lang.org/](http://raa.ruby-lang.org/)

Kind regards

```
robert

```
