On Feb 11, 4:43=A0pm, Adam Groves <adam.gro... / gmail.com> wrote:
> Hi,
>
> I'm banging my head against the wall with the following problem:
>
> array =3D ["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" =3D> nil, "about =3D> {"history =3D> ["part1" =3D> nil, "part2" =
=3D>
> nil]", "company" =3D> nil}}

Your output doesn't seem consistent. (Why an array sometimes but a
hash others?)
Here's something simple and close:

array =3D ["home", "about", "about/history", "about/company", "about/
history/part1", "about/history/part2"]

auto_hash =3D Hash.new{ |h,k| h[k] =3D Hash.new &h.default_proc }

array.each{ |path|
  sub =3D auto_hash
  path.split( "/" ).each{ |dir| sub[dir]; sub =3D sub[dir] }
}
p auto_hash
#=3D> {"about"=3D>{"company"=3D>{}, "history"=3D>{"part1"=3D>{}, "part2"=3D>=
{}}},
"home"=3D>{}}