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
[untyped] 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
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(collection:, index:, location:, comments: []) ⇒ ARef
constructor
A new instance of ARef.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(collection:, index:, location:, comments: []) ⇒ ARef
Returns a new instance of ARef.
478 479 480 481 482 483 |
# File 'lib/syntax_tree/node.rb', line 478 def initialize(collection:, index:, location:, comments: []) @collection = collection @index = index @location = location @comments = comments end |
Instance Attribute Details
#collection ⇒ Object (readonly)
[untyped] the value being indexed
470 471 472 |
# File 'lib/syntax_tree/node.rb', line 470 def collection @collection end |
#comments ⇒ Object (readonly)
[Array[ Comment | EmbDoc ]] the comments attached to this node
476 477 478 |
# File 'lib/syntax_tree/node.rb', line 476 def comments @comments end |
#index ⇒ Object (readonly)
[nil | Args] the value being passed within the brackets
473 474 475 |
# File 'lib/syntax_tree/node.rb', line 473 def index @index end |
Instance Method Details
#child_nodes ⇒ Object Also known as: deconstruct
485 486 487 |
# File 'lib/syntax_tree/node.rb', line 485 def child_nodes [collection, index] end |
#deconstruct_keys(keys) ⇒ Object
491 492 493 494 495 496 497 498 |
# File 'lib/syntax_tree/node.rb', line 491 def deconstruct_keys(keys) { collection: collection, index: index, location: location, comments: comments } end |
#format(q) ⇒ Object
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/syntax_tree/node.rb', line 500 def format(q) q.group do q.format(collection) q.text("[") if index q.indent do q.breakable("") q.format(index) end q.breakable("") end q.text("]") end end |
#pretty_print(q) ⇒ Object
517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/syntax_tree/node.rb', line 517 def pretty_print(q) q.group(2, "(", ")") do q.text("aref") q.breakable q.pp(collection) q.breakable q.pp(index) q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
531 532 533 534 535 536 537 538 539 |
# File 'lib/syntax_tree/node.rb', line 531 def to_json(*opts) { type: :aref, collection: collection, index: index, loc: location, cmts: comments }.to_json(*opts) end |