我将创建一个“Config”类,它在哈希表和树之间起到作用。它只是用于存储具有上下文的全局值。
以下是我的使用方式:
以下是我的使用方式:
Config.get("root.parent.child_b") #=> "value"
这是该类可能的外观:
class Construct
def get(path)
# split path by "."
# search tree for nodes
end
def set(key, value)
# split path by "."
# create tree node if necessary
# set tree value
end
def tree
{
:root => {
:parent => {
:child_a => "value",
:child_b => "another value"
},
:another_parent => {
:something => {
:nesting => "goes on and on"
}
}
}
}
end
end
有没有一个名字来描述这种东西,介于哈希和树之间(我不是计算机科学专业)?基本上是一种将哈希接口应用到树上的方法。
它的输出类似于这样:
t = TreeHash.new
t.set("root.parent.child_a", "value")
t.set("root.parent.child_b", "another value")
期望的输出格式:
t.get("root.parent.child_a") #=> "value"
t.get("root") #=> {"parent" => {"child_a" => "value", "child_b" => "another value"}}
改为这样:
t.get("root") #=> nil
或者这个(通过调用{}.value
获取值)
t.get("root") #=> {"parent" => {"child_a" => {}, "child_b" => {}}}