What is this Hash-like/Tree-like Construct Called?

Posted by viatropos on Stack Overflow See other posts from Stack Overflow or by viatropos
Published on 2010-06-05T04:43:21Z Indexed on 2010/06/05 6:22 UTC
Read the original article Hit count: 199

Filed under:
|
|
|
|

I want to create a "Config" class that acts somewhere between a hash and a tree. It's just for storing global values, which can have a context.

Here's how I use it:

Config.get("root.parent.child_b") #=> "value"

Here's what the class might look like:

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

Is there a name for this kind of thing, somewhere between Hash and Tree (not a Computer Science major)? Basically a hash-like interface to a tree.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about hash