# Nested hash from array of paths

**URL:** <https://www.ruby-forum.com/t/nested-hash-from-array-of-paths/128834>\
**Category:** Ruby\
**Created:** [February 12, 2008, 12:43am UTC](https://www.ruby-forum.com/t/nested-hash-from-array-of-paths/128834 "2008-02-12T00:43:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![addywaddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@addywaddy](https://www.ruby-forum.com/u/addywaddy)\
**Post date:** [February 12, 2008, 12:43am UTC](https://www.ruby-forum.com/t/nested-hash-from-array-of-paths/128834/1 "2008-02-12T00:43:30Z")

</div>

Hi,

I’m banging my head against the wall with the following problem:

array = [“home”, “about”, “about/history”, “about/company”,  
“about/history/part1”, “about/history/part2”]

I want to build a tree from this array in the form of a nested hash:

{“home” =\> nil, "about =\> {“history =\> [“part1” =\> nil, “part2” =\>  
nil]”, “company” =\> nil}}

Any ideas as to how best to solve this?

Regards

Adam

---

<div class="post-metadata">

**Author:** ![addywaddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@addywaddy](https://www.ruby-forum.com/u/addywaddy)\
**Post date:** [February 12, 2008, 1:24am UTC](https://www.ruby-forum.com/t/nested-hash-from-array-of-paths/128834/2 "2008-02-12T01:24:45Z")

</div>

On 2/11/08, Adam G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

I could only figure out how to do it using 2 passes:

tree ={}  
array.sort.each{|w|  
h=tree  
w.split(‘/’).each{|part| h=h[part]||=Hash.new}  
}

def cleanup h  
return true if h.empty?  
h.find\_all{|k,v|h[k]=nil if v.is\_a?(Hash)&&cleanup(v)}  
false  
end

cleanup tree  
p tree  
=\> {“about”=\>{“company”=\>nil, “history”=\>{“part1”=\>nil,  
“part2”=\>nil}}, “home”=\>nil}

-Adam

---

<div class="post-metadata">

**Author:** ![addywaddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@addywaddy](https://www.ruby-forum.com/u/addywaddy)\
**Post date:** [February 12, 2008, 6:01am UTC](https://www.ruby-forum.com/t/nested-hash-from-array-of-paths/128834/3 "2008-02-12T06:01:02Z")

</div>

On Feb 11, 4:43 pm, Adam G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> nil]", “company” =\> nil}}  
> Your output doesn’t seem consistent. (Why an array sometimes but a  
> hash others?)  
> Here’s something simple and close:

array = [“home”, “about”, “about/history”, “about/company”, “about/  
history/part1”, “about/history/part2”]

auto\_hash = Hash.new{ |h,k| h[k] = Hash.new &h.default\_proc }

array.each{ |path|  
sub = auto\_hash  
path.split( “/” ).each{ |dir| sub[dir]; sub = sub[dir] }  
}  
p auto\_hash  
#=\> {“about”=\>{“company”=\>{}, “history”=\>{“part1”=\>{}, “part2”=\>{}}},  
“home”=\>{}}

---

<div class="post-metadata">

**Author:** ![addywaddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@addywaddy](https://www.ruby-forum.com/u/addywaddy)\
**Post date:** [February 12, 2008, 6:07am UTC](https://www.ruby-forum.com/t/nested-hash-from-array-of-paths/128834/4 "2008-02-12T06:07:29Z")

</div>

On Feb 11, 10:00 pm, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> array.each{ |path|  
> sub = auto\_hash  
> path.split( “/” ).each{ |dir| sub[dir]; sub = sub[dir] }  
> }

Bah, of course that can be written as:

array.each{ |path|  
sub = auto\_hash  
path.split( “/” ).each{ |dir| sub = sub[dir] }  
}

---

<div class="post-metadata">

**Author:** ![addywaddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@addywaddy](https://www.ruby-forum.com/u/addywaddy)\
**Post date:** [February 12, 2008, 9:29am UTC](https://www.ruby-forum.com/t/nested-hash-from-array-of-paths/128834/5 "2008-02-12T09:29:32Z")

</div>

Gavin K. wrote:

> On Feb 11, 10:00ï¿½pm, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > array.each{ |path|  
> > ï¿½ sub = auto\_hash  
> > ï¿½ path.split( “/” ).each{ |dir| sub[dir]; sub = sub[dir] }  
> > }
> 
> Bah, of course that can be written as:
> 
> array.each{ |path|  
> sub = auto\_hash  
> path.split( “/” ).each{ |dir| sub = sub[dir] }  
> }

Gavin,

thanks mate - works a charm. I’m glad you spotted my deliberately  
inconsistent output as well! 😉

Adam

---

<div class="post-metadata">

**Author:** ![addywaddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@addywaddy](https://www.ruby-forum.com/u/addywaddy)\
**Post date:** [February 12, 2008, 3:13pm UTC](https://www.ruby-forum.com/t/nested-hash-from-array-of-paths/128834/6 "2008-02-12T15:13:23Z")

</div>

2008/2/12, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> path.split( “/” ).each{ |dir| sub = sub[dir] }  
> }

Or even

array.each{ |path|  
path.to\_enum(:split, “/” ).inject(auto\_hash){ |sub, dir| sub[dir] }  
}

Full credits go to you for this line alone:

auto\_hash = Hash.new{ |h,k| h[k] = Hash.new &h.default\_proc }

Never occurred to me to use this nice one line approach instead of the  
usual two line version (first create block then use it). Thanks a  
lot, this is really a gem!

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![addywaddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@addywaddy](https://www.ruby-forum.com/u/addywaddy)\
**Post date:** [February 12, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/nested-hash-from-array-of-paths/128834/7 "2008-02-12T15:35:54Z")

</div>

On Feb 12, 7:12 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Full credits go to you for this line alone:
> 
> auto\_hash = Hash.new{ |h,k| h[k] = Hash.new &h.default\_proc }
> 
> Never occurred to me to use this nice one line approach instead of the  
> usual two line version (first create block then use it). Thanks a  
> lot, this is really a gem!

I wish I could claim those credits. I had always used the two-line  
version two until Jens W. posted this gem in [ruby-talk:288946]

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/288946](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/288946)

---

<div class="post-metadata">

**Author:** ![addywaddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@addywaddy](https://www.ruby-forum.com/u/addywaddy)\
**Post date:** [July 30, 2013, 7:58pm UTC](https://www.ruby-forum.com/t/nested-hash-from-array-of-paths/128834/8 "2013-07-30T19:58:38Z")

</div>

Alternatively:

require ‘xkeys’  
array = [“home”, “about”, “about/history”, “about/company”,  
“about/history/part1”, “about/history/part2”]  
tree = {}.extend XKeys::Hash  
array.each { |path| tree[\*(path.split ?/)] = nil }  
p tree

# {“home”=\>nil, “about”=\>{“history”=\>{“part1”=\>nil, “part2”=\>nil},

# “company”=\>nil}}

The leaves are nil (not empty hashes), and the code is slightly less  
convoluted.

---

<div class="post-metadata">

**Author:** ![addywaddy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@addywaddy](https://www.ruby-forum.com/u/addywaddy)\
**Post date:** [February 12, 2008, 10:47pm UTC](https://www.ruby-forum.com/t/nested-hash-from-array-of-paths/128834/9 "2008-02-12T22:47:21Z")

</div>

On 12.02.2008 15:30, Phrogz wrote:

> version two until Jens W. posted this gem in [ruby-talk:288946]
> 
> [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/288946](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/288946)

Ok, then you get at least part of the credits for bringing it up again.  
😉

Kind regards

robert
