2

我有这个路径数组:

[
 "Shape Up.Introduction.Growing Pains",
 "Shape Up.Shaping.Principles of Shaping.Wireframes are too concrete",
 "Shape Up.Shaping.Principles of Shaping.Words are too abstract",
 "Shape Up.Introduction.Six-week cycles",
 "Shape Up.Shaping.Steps to shaping"
]

每个段用 . 分隔.

我需要在视图中呈现目录。

将数组转换为嵌套哈希的最简单方法是什么?或者也许有更好的结构/方法来生成所需的html?

UPD

[
 "Shape Up.Introduction.Growing Pains",
 "Shape Up.Shaping.Principles of Shaping.Wireframes are too concrete",
 "Shape Up.Shaping.Principles of Shaping.Words are too abstract",
 "Shape Up.Introduction.Six-week cycles",
 "Shape Up.Shaping.Steps to shaping"
].inject({}) {|h,i|
  t = h; i.split(".").each {|n| t[n] ||= {}; t = t[n]}; h
}

给出我需要的哈希树结构:

{
    "Shape Up" => {
        "Introduction" => {
              "Growing Pains" => {},
            "Six-week cycles" => {}
        },
             "Shaping" => {
            "Principles of Shaping" => {
                "Wireframes are too concrete" => {},
                     "Words are too abstract" => {}
            },
                 "Steps to shaping" => {}
        }
    }
}
4

0 回答 0