ハッシュとツリーの間のどこかで機能する「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" => {}}}