Ruby On Rails:将字符串中的路径转换为递归散列

时间:2021-12-03 21:44:00

Is somebody able to suggest me a way to do this ? ( all is in the subject :D )

有人能给我建议一个方法吗?(题目为:D)

what I want is using a "path" and tranform it as a suit of sub key,

我想要的是用一条路径把它转化成一套子键,

e.g. : I have that params: path = "earth/animal/human/men/young/" value = "martin" and I want :

例如:我有那个params: path = "earth/animal/human/men/young/" value = "martin",我想:

  `Global_hash = { earth => { human => { men => { young => "martin"
                                                }
                                       }
                            }
                 }`

path = "earth/animal/human/men/old/" value = "John" and I want :

路径=“地球/动物/人类/人/老人/价值=”约翰”,我想:

 Global_hash = { earth => { human => { men => { young => "martin",
                                                old   =>  "John" 
                                         }
                                }
                     }
          }

add an other

添加一个其他

path = "earth/animal/human/women/old/" value = "Eve" and I want :

路径=“地球/动物/人类/女人/旧/”值=“夏娃”,我想:

`Global_hash = { earth => { human => { men   => { young => "martin",
                                                  old      =>  "John"
                                                },
                                       women => { old => "Eve"
                                                }
                                     }
                          }
               }

`

The final goal is a way to produce yml file with 2 parameters : the path and the value

最终目标是生成具有两个参数的yml文件:路径和值

the exemple produces : `

这个例子产生:'

earth:
  animal:
    human:
      men:    
        young: "martin"
        old: "John"
      women:
        old: "Eve"

` it will allow us to have a yml file with all object sort by sections thanks to their path.

由于它们的路径,它将允许我们拥有一个包含所有对象排序的yml文件。

Thanks per advance

由于每前进

2 个解决方案

#1


5  

path = 'earth/animal/human/men/young/'
value = 'martin'
path.split('/').reverse.reduce(value){ |r, e| {e.to_sym => r} }

#2


1  

Functional recursive approach:

函数递归的方法:

def insert(hash, path, value)
  head, *tail = path
  if tail.empty?
    hash.merge(head => value)
  else
    h = insert(hash[head] || {}, tail, value)
    hash.merge(head => hash.has_key?(head) ? hash[head].merge(h) : h)
  end
end

h1 = insert({}, "animal/human/women/old".split("/"), "Eve")
# {"animal"=>{"human"=>{"women"=>{"old"=>"Eve"}}}}

h2 = insert(h1, "animal/human/men/old".split("/"), "Adam")
# {"animal"=>{"human"=>{"women"=>{"old"=>"Eve"}, 
#                       "men"=>{"old"=>"Adam"}}}}

h3 = insert(h2, "animal/chimpanzee/smart".split("/"), "Caesar")
# {"animal"=>{"human"=>{"women"=>{"old"=>"Eve"}, 
#                       "men"=>{"old"=>"Adam"}}, 
#             "chimpanzee"=>{"smart"=>"Caesar"}}}

#1


5  

path = 'earth/animal/human/men/young/'
value = 'martin'
path.split('/').reverse.reduce(value){ |r, e| {e.to_sym => r} }

#2


1  

Functional recursive approach:

函数递归的方法:

def insert(hash, path, value)
  head, *tail = path
  if tail.empty?
    hash.merge(head => value)
  else
    h = insert(hash[head] || {}, tail, value)
    hash.merge(head => hash.has_key?(head) ? hash[head].merge(h) : h)
  end
end

h1 = insert({}, "animal/human/women/old".split("/"), "Eve")
# {"animal"=>{"human"=>{"women"=>{"old"=>"Eve"}}}}

h2 = insert(h1, "animal/human/men/old".split("/"), "Adam")
# {"animal"=>{"human"=>{"women"=>{"old"=>"Eve"}, 
#                       "men"=>{"old"=>"Adam"}}}}

h3 = insert(h2, "animal/chimpanzee/smart".split("/"), "Caesar")
# {"animal"=>{"human"=>{"women"=>{"old"=>"Eve"}, 
#                       "men"=>{"old"=>"Adam"}}, 
#             "chimpanzee"=>{"smart"=>"Caesar"}}}