해시와 트리 사이에서 작동하는 "구성"클래스를 만들고 싶습니다. 컨텍스트를 가질 수있는 전역 값을 저장하기위한 것입니다. 여기Hash-Like/Tree와 같은 Construct Called는 무엇입니까?
이다 나는 그것을 사용하는 방법 :
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
는 이런 종류의 이름이 거기에 (어딘가에 해시와 나무 사이에하는하지 :
여기
Config.get("root.parent.child_b") #=> "value"
가 같은 클래스가 보일 수 있습니다 무엇 컴퓨터 과학 전공)? 기본적으로 나무에 대한 해시 같은 인터페이스. 이렇게 출력
예시 :
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" => {}}}
이것은 굉장합니다. –
어떤 아이디어를 추가하는 방법 a) 리프 노드는 해시가 아니며, 값 (기본적으로'attr_accessor : value'를 제거하는 것을 의미 함)과 b)'get ("root")'또는 어떤 레벨 반환 잎 노드가 아닌 경우 아래의 트리가 null 대신? 이를 구현하려고 시도하지만 많은 코드/복잡성을 추가하고 있습니다. 아마 하나의 라이너 트릭을 알고있을 것입니다. 샘플 출력으로 질문을 업데이트했습니다. –
나는 코드를 제거하는 대신에 다음을 추가하는 대신에 – samuil