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

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



2494
2495
2496
2497
2498
2499
# File 'lib/prism/node.rb', line 2494

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



2491
2492
2493
# File 'lib/prism/node.rb', line 2491

def operator_loc
  @operator_loc
end

#targetObject (readonly)

attr_reader target: Node



2488
2489
2490
# File 'lib/prism/node.rb', line 2488

def target
  @target
end

#valueObject (readonly)

attr_reader value: Node



2485
2486
2487
# File 'lib/prism/node.rb', line 2485

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



2502
2503
2504
# File 'lib/prism/node.rb', line 2502

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

#child_nodesObject Also known as: deconstruct

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



2507
2508
2509
# File 'lib/prism/node.rb', line 2507

def child_nodes
  [value, target]
end

#comment_targetsObject

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



2517
2518
2519
# File 'lib/prism/node.rb', line 2517

def comment_targets
  [value, target, operator_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



2512
2513
2514
# File 'lib/prism/node.rb', line 2512

def compact_child_nodes
  [value, target]
end

#copy(**params) ⇒ Object

def copy: (**params) -> CapturePatternNode



2522
2523
2524
2525
2526
2527
2528
2529
# File 'lib/prism/node.rb', line 2522

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 | String | Token | Array | Location]



2535
2536
2537
# File 'lib/prism/node.rb', line 2535

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

#inspect(inspector = NodeInspector.new) ⇒ Object



2544
2545
2546
2547
2548
2549
2550
2551
2552
# File 'lib/prism/node.rb', line 2544

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



2540
2541
2542
# File 'lib/prism/node.rb', line 2540

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



2568
2569
2570
# File 'lib/prism/node.rb', line 2568

def type
  :capture_pattern_node
end