Module: Tree::Node::InstanceMethods
- Defined in:
- lib/tree_node.rb
Instance Attribute Summary collapse
- #children ⇒ Object
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #add_child(object) ⇒ Object
- #map(direction, collecting_object = [], &block) ⇒ Object
- #root(&block) ⇒ Object
Instance Attribute Details
#children ⇒ Object
29 30 31 |
# File 'lib/tree_node.rb', line 29 def children @children ||= [] end |
#parent ⇒ Object
Returns the value of attribute parent.
11 12 13 |
# File 'lib/tree_node.rb', line 11 def parent @parent end |
Instance Method Details
#add_child(object) ⇒ Object
33 34 35 36 |
# File 'lib/tree_node.rb', line 33 def add_child object object.parent = self children << object end |
#map(direction, collecting_object = [], &block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/tree_node.rb', line 14 def map(direction, collecting_object = [], &block) if direction == :down collecting_object << block.call(self) children.each { |child| child.map(direction, collecting_object, &block) } else parent.map(direction, collecting_object, &block) if parent collecting_object << block.call(self) end collecting_object end |
#root(&block) ⇒ Object
25 26 27 |
# File 'lib/tree_node.rb', line 25 def root(&block) parent.root(&block) rescue block.call(self) end |