Class: RubySGF::SGF::Node

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

Overview

– this class is partially inspired by code written by Stefan Rusterholz, stefan.rusterholz <AT> gmail <DOT> com ++

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, properties = []) ⇒ Node

Returns a new instance of Node.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sgf/node.rb', line 17

def initialize(parent, properties = [])
  @parent = parent
  @properties = properties
  @children = []
  
  @parent << self if parent
  
  if parent
    @number = parent.number + 1
  else
    @number = 0
  end
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



14
15
16
# File 'lib/sgf/node.rb', line 14

def children
  @children
end

#numberObject (readonly)

Returns the value of attribute number.



14
15
16
# File 'lib/sgf/node.rb', line 14

def number
  @number
end

#parentObject (readonly)

Returns the value of attribute parent.



14
15
16
# File 'lib/sgf/node.rb', line 14

def parent
  @parent
end

#propertiesObject

Returns the value of attribute properties.



15
16
17
# File 'lib/sgf/node.rb', line 15

def properties
  @properties
end

Instance Method Details

#<<(node) ⇒ Object



43
44
45
# File 'lib/sgf/node.rb', line 43

def <<(node)
  @children << node
end

#[](index) ⇒ Object



39
40
41
# File 'lib/sgf/node.rb', line 39

def [](index)
  @children[index]
end

#leaf?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/sgf/node.rb', line 47

def leaf?
  @children.empty?
end

#nextObject



35
36
37
# File 'lib/sgf/node.rb', line 35

def next
  @children[0]
end

#prevObject



31
32
33
# File 'lib/sgf/node.rb', line 31

def prev
  @parent
end

#root?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/sgf/node.rb', line 51

def root?
  @parent.nil?
end

#to_sObject



55
56
57
# File 'lib/sgf/node.rb', line 55

def to_s
  " "*@number << @number.to_s
end

#write_tree(string = "") ⇒ Object



59
60
61
62
63
64
65
# File 'lib/sgf/node.rb', line 59

def write_tree(string="")
  string << self.to_s << "\n"
  @children.each do |child|
    child.write_tree(string)
  end
  string
end