Class: BracketNotation::Identifier
- Inherits:
-
Expression
- Object
- Expression
- BracketNotation::Identifier
- Defined in:
- lib/bracket_notation/expressions/identifier.rb
Overview
This class represents an identifier expression. Identifers are expressions that can have children – branch nodes, in other words.
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#depth ⇒ Object
Returns the value of attribute depth.
Attributes inherited from Expression
Instance Method Summary collapse
-
#add_child(expression) ⇒ Object
Adds the given expression to the children array and updates the new child’s parent and depth attributes.
-
#initialize(value, depth = 0) ⇒ Identifier
constructor
Saves the identifier value and optional tree depth.
-
#pretty_print ⇒ Object
Prints a visual representation of the syntax tree.
Constructor Details
#initialize(value, depth = 0) ⇒ Identifier
Saves the identifier value and optional tree depth.
36 37 38 39 |
# File 'lib/bracket_notation/expressions/identifier.rb', line 36 def initialize(value, depth = 0) super @children = [] end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
33 34 35 |
# File 'lib/bracket_notation/expressions/identifier.rb', line 33 def children @children end |
#depth ⇒ Object
Returns the value of attribute depth.
33 34 35 |
# File 'lib/bracket_notation/expressions/identifier.rb', line 33 def depth @depth end |
Instance Method Details
#add_child(expression) ⇒ Object
Adds the given expression to the children array and updates the new child’s parent and depth attributes.
43 44 45 46 47 |
# File 'lib/bracket_notation/expressions/identifier.rb', line 43 def add_child(expression) expression.parent = self expression.depth = @depth + 1 @children << expression end |
#pretty_print ⇒ Object
Prints a visual representation of the syntax tree.
56 57 58 59 60 |
# File 'lib/bracket_notation/expressions/identifier.rb', line 56 def pretty_print out = super @children.each {|child| out << child.pretty_print} out end |