Class: Prism::MatchWriteNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
ext/prism/api_node.c

Overview

Represents writing local variables using a regular expression match with named capture groups.

/(?<foo>bar)/ =~ baz
^^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(call, locals, location) ⇒ MatchWriteNode

def initialize: (call: CallNode, locals: Array, location: Location) -> void



9741
9742
9743
9744
9745
# File 'lib/prism/node.rb', line 9741

def initialize(call, locals, location)
  @call = call
  @locals = locals
  @location = location
end

Instance Attribute Details

#callObject (readonly)

attr_reader call: CallNode



9735
9736
9737
# File 'lib/prism/node.rb', line 9735

def call
  @call
end

#localsObject (readonly)

attr_reader locals: Array



9738
9739
9740
# File 'lib/prism/node.rb', line 9738

def locals
  @locals
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



9748
9749
9750
# File 'lib/prism/node.rb', line 9748

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



9753
9754
9755
# File 'lib/prism/node.rb', line 9753

def child_nodes
  [call]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



9763
9764
9765
# File 'lib/prism/node.rb', line 9763

def comment_targets
  [call]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



9758
9759
9760
# File 'lib/prism/node.rb', line 9758

def compact_child_nodes
  [call]
end

#copy(**params) ⇒ Object

def copy: (**params) -> MatchWriteNode



9768
9769
9770
9771
9772
9773
9774
# File 'lib/prism/node.rb', line 9768

def copy(**params)
  MatchWriteNode.new(
    params.fetch(:call) { call },
    params.fetch(:locals) { locals },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



9780
9781
9782
# File 'lib/prism/node.rb', line 9780

def deconstruct_keys(keys)
  { call: call, locals: locals, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



9784
9785
9786
9787
9788
9789
9790
# File 'lib/prism/node.rb', line 9784

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── call:\n"
  inspector << inspector.child_node(call, "│   ")
  inspector << "└── locals: #{locals.inspect}\n"
  inspector.to_str
end

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



9806
9807
9808
# File 'lib/prism/node.rb', line 9806

def type
  :match_write_node
end