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.



11687
11688
11689
11690
11691
# File 'lib/syntax_tree/node.rb', line 11687

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#valueObject (readonly)

Const | CVar | GVar | Ident | IVar

the value of this node



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

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



11725
11726
11727
# File 'lib/syntax_tree/node.rb', line 11725

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



11697
11698
11699
# File 'lib/syntax_tree/node.rb', line 11697

def child_nodes
  [value]
end

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



11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
# File 'lib/syntax_tree/node.rb', line 11701

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



11714
11715
11716
# File 'lib/syntax_tree/node.rb', line 11714

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

#format(q) ⇒ Object



11718
11719
11720
11721
11722
11723
# File 'lib/syntax_tree/node.rb', line 11718

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