我在Ruby中具有以下结构:
[
{“type"=>"Action", "parameterized"=>false, "groups"=>["feed"], "value"=>"feed”},
{"type"=>"State", "parameterized"=>true, "groups"=>["group"], "value"=>"group:%1s”},
{"type"=>"Action", "parameterized"=>false, "groups"=>["challenge", "build"], "value"=>"challenge:build”},
{"type"=>"Action", "parameterized"=>false, "groups"=>["challenge", "decorate"], "value"=>"challenge:decorate”},
{"type"=>"Action", "parameterized"=>false, "groups"=>["report"], "value"=>"report”},
{"type"=>"Action", "parameterized"=>false, "groups"=>["report", "complete"], "value"=>"report:complete”},
]
数组中的元素可以按任何顺序排列,我希望将其解析为树结构,以便在有匹配组的情况下(每个组数组按顺序排列,其中第一个是根,第二个是根第一个和第二个的子项是之前一个的子项,依此类推),然后合并到树中的同一分支中。如果存在“已参数化”参数,则这还会在树中导致额外的分支。我希望上面的结构最后一行如下:
{
"Action" => {
"feed" => {
"value" => "feed"
},
"challenge" => {
"build" => {
"value" => "challenge:build"
}
"decorate" => {
"value" => "challenge:decorate"
}
},
"report" => {
"value" => "report",
"complete" => {
"value" => "report:complete"
}
}
},
"State" => {
"group" => {
"parameterized" => {
"value" => "group:%1s"
}
}
}
}
任何想法。如何在Ruby中做到这一点?
最诚挚的问候
索伦
答案 0 :(得分:0)
首先,让我们定义将在同一类型内找到掩埋组的过程
docker exec -it -v ./myscript.sh:/myscript.sh server0 /myscript.sh
现在,我们准备遍历输入哈希,将尾部添加到 found 键或插入新键。
Ruby中的对象是可变的,这将对我们有很大帮助;遍历野兽,我们将检测到所需的哈希并对其进行修改。这种变化将整体反映出来。
./start.sh
NB 带有累加器默认proc(deep_find = ->(hash, name) {
hash.detect { |k, v| k == name || v.is_a?(Hash) && deep_find.(v, name) }
}
)的技巧使您不必担心新值的初始化,无论它们的深度如何。