Class: Prism::CapturePatternNode

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

Overview

Represents assigning to a local variable in pattern matching.

foo => [bar => baz]
       ^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, target, operator_loc, location) ⇒ CapturePatternNode

def initialize: (value: Node, target: Node, operator_loc: Location, location: Location) -> void



2652
2653
2654
2655
2656
2657
# File 'lib/prism/node.rb', line 2652

def initialize(value, target, operator_loc, location)
  @value = value
  @target = target
  @operator_loc = operator_loc
  @location = location
end

Instance Attribute Details

#operator_locObject (readonly)

attr_reader operator_loc: Location



2649
2650
2651
# File 'lib/prism/node.rb', line 2649

def operator_loc
  @operator_loc
end

#targetObject (readonly)

attr_reader target: Node



2646
2647
2648
# File 'lib/prism/node.rb', line 2646

def target
  @target
end

#valueObject (readonly)

attr_reader value: Node



2643
2644
2645
# File 'lib/prism/node.rb', line 2643

def value
  @value
end

Class Method Details

.typeObject

Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.

def self.type: () -> Symbol



2737
2738
2739
# File 'lib/prism/node.rb', line 2737

def self.type
  :capture_pattern_node
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



2660
2661
2662
# File 'lib/prism/node.rb', line 2660

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

#child_nodesObject Also known as: deconstruct

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



2665
2666
2667
# File 'lib/prism/node.rb', line 2665

def child_nodes
  [value, target]
end

#comment_targetsObject

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



2675
2676
2677
# File 'lib/prism/node.rb', line 2675

def comment_targets
  [value, target, operator_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



2670
2671
2672
# File 'lib/prism/node.rb', line 2670

def compact_child_nodes
  [value, target]
end

#copy(**params) ⇒ Object

def copy: (**params) -> CapturePatternNode



2680
2681
2682
2683
2684
2685
2686
2687
# File 'lib/prism/node.rb', line 2680

def copy(**params)
  CapturePatternNode.new(
    params.fetch(:value) { value },
    params.fetch(:target) { target },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



2693
2694
2695
# File 'lib/prism/node.rb', line 2693

def deconstruct_keys(keys)
  { value: value, target: target, operator_loc: operator_loc, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object

def inspect(inspector: NodeInspector) -> String



2703
2704
2705
2706
2707
2708
2709
2710
2711
# File 'lib/prism/node.rb', line 2703

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

#operatorObject

def operator: () -> String



2698
2699
2700
# File 'lib/prism/node.rb', line 2698

def operator
  operator_loc.slice
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



2727
2728
2729
# File 'lib/prism/node.rb', line 2727

def type
  :capture_pattern_node
end