Class: SyntaxTree::PinnedVarRef

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

PinnedVarRef represents a pinned variable reference within a pattern matching pattern.

case value
in ^variable
end

This can be a plain local variable like the example above. It can also be a a class variable, a global variable, or an instance variable.

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(value:, location:) ⇒ PinnedVarRef

Returns a new instance of PinnedVarRef.



11684
11685
11686
11687
11688
# File 'lib/syntax_tree/node.rb', line 11684

def initialize(value:, location:)
  @value = value
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11682
11683
11684
# File 'lib/syntax_tree/node.rb', line 11682

def comments
  @comments
end

#valueObject (readonly)

Const | CVar | GVar | Ident | IVar

the value of this node



11679
11680
11681
# File 'lib/syntax_tree/node.rb', line 11679

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



11722
11723
11724
# File 'lib/syntax_tree/node.rb', line 11722

def ===(other)
  other.is_a?(PinnedVarRef) && value === other.value
end

#accept(visitor) ⇒ Object



11690
11691
11692
# File 'lib/syntax_tree/node.rb', line 11690

def accept(visitor)
  visitor.visit_pinned_var_ref(self)
end

#child_nodesObject Also known as: deconstruct



11694
11695
11696
# File 'lib/syntax_tree/node.rb', line 11694

def child_nodes
  [value]
end

#copy(value: nil, location: nil) ⇒ Object



11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
# File 'lib/syntax_tree/node.rb', line 11698

def copy(value: nil, location: nil)
  node =
    PinnedVarRef.new(
      value: value || self.value,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



11711
11712
11713
# File 'lib/syntax_tree/node.rb', line 11711

def deconstruct_keys(_keys)
  { value: value, location: location, comments: comments }
end

#format(q) ⇒ Object



11715
11716
11717
11718
11719
11720
# File 'lib/syntax_tree/node.rb', line 11715

def format(q)
  q.group do
    q.text("^")
    q.format(value)
  end
end