Class: CompSci::Node
- Inherits:
-
Object
- Object
- CompSci::Node
- Defined in:
- lib/compsci/node.rb
Overview
has a value and an array of children; allows child gaps
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(value, children: []) ⇒ Node
constructor
A new instance of Node.
- #inspect ⇒ Object
-
#set_child(idx, node) ⇒ Object
This could be done directly with self.children, but #set_child is part of the Node API.
- #to_s ⇒ Object
Constructor Details
#initialize(value, children: []) ⇒ Node
Returns a new instance of Node.
7 8 9 10 11 12 13 14 15 |
# File 'lib/compsci/node.rb', line 7 def initialize(value, children: []) @value = value if children.is_a?(Integer) @children = Array.new(children) else @children = children end # @metadata = {} end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
5 6 7 |
# File 'lib/compsci/node.rb', line 5 def children @children end |
#value ⇒ Object
Returns the value of attribute value.
4 5 6 |
# File 'lib/compsci/node.rb', line 4 def value @value end |
Instance Method Details
#inspect ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/compsci/node.rb', line 27 def inspect "#<%s:0x%0xi @value=%s @children=[%s]>" % [self.class, self.object_id, self.to_s, @children.map(&:to_s).join(', ')] end |
#set_child(idx, node) ⇒ Object
This could be done directly with self.children, but #set_child is part of the Node API.
23 24 25 |
# File 'lib/compsci/node.rb', line 23 def set_child(idx, node) @children[idx] = node end |
#to_s ⇒ Object
17 18 19 |
# File 'lib/compsci/node.rb', line 17 def to_s @value.to_s end |