Sha256: 150ffa1a354401f22b95bc3b48fbb3286795c80a3fc8b7f79eeba51f9b0f83f1
Contents?: true
Size: 857 Bytes
Versions: 2
Compression:
Stored size: 857 Bytes
Contents
# A basic representation of a Redwood tree. module Redwood class Node include Redwood attr_reader :name attr_accessor :value def initialize(name=nil, parent=nil) @name = name @parent = parent end # Creates a child, adds it to children, and returns the child def add_child(name) child = self.class.new(name, self) yield child if block_given? children << child child end # Add a node to this nodes children, and return the child def <<(child) child.instance_variable_set(:@parent, self) children << child child end # Lookup a child node by its name def [](key) selected_child = children.select {|child| child.name == key } selected_child.size.eql?(1) ? selected_child.first : selected_child end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
redwood-0.1.1 | lib/redwood/node.rb |
redwood-0.1.0 | lib/redwood/node.rb |