Module: Tree::InternalChildrenListImplementation

Includes:
InternalChildrenImplementation
Defined in:
lib/modular_tree/implementations.rb

Overview

Demonstrates a linked list implementation

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InternalChildrenImplementation

#branches, #each_branch

Methods included from Implementation

#initialize

Methods included from NodeProperty

#node, #node_value

Methods included from BranchesProperty

#bare?, #branches, #each_branch

Methods included from ChildrenProperty

#detach

Methods included from InternalImplementation

#node, #node_value, #this

Instance Attribute Details

#first_childObject

Returns the value of attribute first_child.



136
137
138
# File 'lib/modular_tree/implementations.rb', line 136

def first_child
  @first_child
end

#next_siblingObject

Returns the value of attribute next_sibling.



137
138
139
# File 'lib/modular_tree/implementations.rb', line 137

def next_sibling
  @next_sibling
end

Instance Method Details

#attach(child) ⇒ Object



152
153
154
155
# File 'lib/modular_tree/implementations.rb', line 152

def attach(child)
  child.instance_variable_set(:@next_sibling, first_child)
  @first_child = child
end

#childrenObject



139
140
141
142
143
144
# File 'lib/modular_tree/implementations.rb', line 139

def children
  n = self.first_child or return []
  a = [n]
  a << n while n = n.next_sibling
  a
end

#each_child {|curr| ... } ⇒ Object

Yields:

  • (curr)


146
147
148
149
150
# File 'lib/modular_tree/implementations.rb', line 146

def each_child(&block)
  curr = first_child or return
  yield(curr)
  yield curr while curr = next_sibling
end