Class: SyntaxTree::For
Overview
For represents using a for loop.
for value in list do
end
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
- Node
-
the object being enumerated in the loop.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#index ⇒ Object
readonly
- MLHS | VarField
-
the variable declaration being used to pull values out of the object being enumerated.
-
#statements ⇒ Object
readonly
- Statements
-
the statements to be executed.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(index: nil, collection: nil, statements: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(index:, collection:, statements:, location:) ⇒ For
constructor
A new instance of For.
Methods inherited from Node
#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(index:, collection:, statements:, location:) ⇒ For
Returns a new instance of For.
5545 5546 5547 5548 5549 5550 5551 |
# File 'lib/syntax_tree/node.rb', line 5545 def initialize(index:, collection:, statements:, location:) @index = index @collection = collection @statements = statements @location = location @comments = [] end |
Instance Attribute Details
#collection ⇒ Object (readonly)
- Node
-
the object being enumerated in the loop
5537 5538 5539 |
# File 'lib/syntax_tree/node.rb', line 5537 def collection @collection end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
5543 5544 5545 |
# File 'lib/syntax_tree/node.rb', line 5543 def comments @comments end |
#index ⇒ Object (readonly)
- MLHS | VarField
-
the variable declaration being used to
pull values out of the object being enumerated
5534 5535 5536 |
# File 'lib/syntax_tree/node.rb', line 5534 def index @index end |
#statements ⇒ Object (readonly)
- Statements
-
the statements to be executed
5540 5541 5542 |
# File 'lib/syntax_tree/node.rb', line 5540 def statements @statements end |
Instance Method Details
#===(other) ⇒ Object
5605 5606 5607 5608 |
# File 'lib/syntax_tree/node.rb', line 5605 def ===(other) other.is_a?(For) && index === other.index && collection === other.collection && statements === other.statements end |
#accept(visitor) ⇒ Object
5553 5554 5555 |
# File 'lib/syntax_tree/node.rb', line 5553 def accept(visitor) visitor.visit_for(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
5557 5558 5559 |
# File 'lib/syntax_tree/node.rb', line 5557 def child_nodes [index, collection, statements] end |
#copy(index: nil, collection: nil, statements: nil, location: nil) ⇒ Object
5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 |
# File 'lib/syntax_tree/node.rb', line 5561 def copy(index: nil, collection: nil, statements: nil, location: nil) node = For.new( index: index || self.index, collection: collection || self.collection, statements: statements || self.statements, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
5576 5577 5578 5579 5580 5581 5582 5583 5584 |
# File 'lib/syntax_tree/node.rb', line 5576 def deconstruct_keys(_keys) { index: index, collection: collection, statements: statements, location: location, comments: comments } end |
#format(q) ⇒ Object
5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 |
# File 'lib/syntax_tree/node.rb', line 5586 def format(q) q.group do q.text("for ") q.group { q.format(index) } q.text(" in ") q.format(collection) unless statements.empty? q.indent do q.breakable_force q.format(statements) end end q.breakable_force q.text("end") end end |