Class: TreeBranch::Node
- Inherits:
-
Object
- Object
- TreeBranch::Node
- Defined in:
- lib/tree_branch/node.rb
Overview
Main class the outlines the basic operations and structure of a node in the tree.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #add(*children_to_add) ⇒ Object
- #eql?(other) ⇒ Boolean
-
#initialize(data) ⇒ Node
constructor
A new instance of Node.
- #to_s ⇒ Object
Constructor Details
#initialize(data) ⇒ Node
Returns a new instance of Node.
15 16 17 18 |
# File 'lib/tree_branch/node.rb', line 15 def initialize(data) @data = data @children = [] end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
13 14 15 |
# File 'lib/tree_branch/node.rb', line 13 def children @children end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
13 14 15 |
# File 'lib/tree_branch/node.rb', line 13 def data @data end |
Instance Method Details
#==(other) ⇒ Object
34 35 36 |
# File 'lib/tree_branch/node.rb', line 34 def ==(other) eql?(other) end |
#add(*children_to_add) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/tree_branch/node.rb', line 20 def add(*children_to_add) children_to_add.flatten.each do |child| raise ArgumentError, "Improper class: #{child.class.name}" unless child.is_a?(self.class) @children << child end self end |
#eql?(other) ⇒ Boolean
30 31 32 |
# File 'lib/tree_branch/node.rb', line 30 def eql?(other) data == other.data && children == other.children end |
#to_s ⇒ Object
38 39 40 |
# File 'lib/tree_branch/node.rb', line 38 def to_s "[#{self.class.name}] Data: #{data}, Child Count: #{children.length}" end |