Class: CompSci::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/compsci/node.rb

Overview

has a value and an array of children; allows child gaps

Direct Known Subclasses

ChildNode, FlexNode, KeyNode

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/compsci/node.rb', line 5

def children
  @children
end

#valueObject

Returns the value of attribute value.



4
5
6
# File 'lib/compsci/node.rb', line 4

def value
  @value
end

Instance Method Details

#inspectObject



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_sObject



17
18
19
# File 'lib/compsci/node.rb', line 17

def to_s
  @value.to_s
end