Class: SyntaxTree::ARef
Overview
ARef represents when you’re pulling a value out of a collection at a specific index. Put another way, it’s any time you’re calling the method #[].
collection[index]
The nodes usually contains two children, the collection and the index. In some cases, you don’t necessarily have the second child node, because you can call procs with a pretty esoteric syntax. In the following example, you wouldn’t have a second child node:
collection[]
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
- Node
-
the value being indexed.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#index ⇒ Object
readonly
- nil | Args
-
the value being passed within the brackets.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(collection: nil, index: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(collection:, index:, location:) ⇒ ARef
constructor
A new instance of ARef.
Methods inherited from Node
#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(collection:, index:, location:) ⇒ ARef
Returns a new instance of ARef.
577 578 579 580 581 582 |
# File 'lib/syntax_tree/node.rb', line 577 def initialize(collection:, index:, location:) @collection = collection @index = index @location = location @comments = [] end |
Instance Attribute Details
#collection ⇒ Object (readonly)
- Node
-
the value being indexed
569 570 571 |
# File 'lib/syntax_tree/node.rb', line 569 def collection @collection end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
575 576 577 |
# File 'lib/syntax_tree/node.rb', line 575 def comments @comments end |
#index ⇒ Object (readonly)
- nil | Args
-
the value being passed within the brackets
572 573 574 |
# File 'lib/syntax_tree/node.rb', line 572 def index @index end |
Instance Method Details
#===(other) ⇒ Object
632 633 634 635 |
# File 'lib/syntax_tree/node.rb', line 632 def ===(other) other.is_a?(ARef) && collection === other.collection && index === other.index end |
#accept(visitor) ⇒ Object
584 585 586 |
# File 'lib/syntax_tree/node.rb', line 584 def accept(visitor) visitor.visit_aref(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
588 589 590 |
# File 'lib/syntax_tree/node.rb', line 588 def child_nodes [collection, index] end |
#copy(collection: nil, index: nil, location: nil) ⇒ Object
592 593 594 595 596 597 598 599 600 601 602 |
# File 'lib/syntax_tree/node.rb', line 592 def copy(collection: nil, index: nil, location: nil) node = ARef.new( collection: collection || self.collection, index: index || self.index, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
606 607 608 609 610 611 612 613 |
# File 'lib/syntax_tree/node.rb', line 606 def deconstruct_keys(_keys) { collection: collection, index: index, location: location, comments: comments } end |
#format(q) ⇒ Object
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 |
# File 'lib/syntax_tree/node.rb', line 615 def format(q) q.group do q.format(collection) q.text("[") if index q.indent do q.breakable_empty q.format(index) end q.breakable_empty end q.text("]") end end |