Class: Prism::ArrayPatternNode

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

Overview

Represents an array pattern in pattern matching.

foo in 1, 2
^^^^^^^^^^^

foo in [1, 2]
^^^^^^^^^^^^^

foo in *1
^^^^^^^^^

foo in Bar[]
^^^^^^^^^^^^

foo in Bar[1, 2, 3]
^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constant, requireds, rest, posts, opening_loc, closing_loc, location) ⇒ ArrayPatternNode

def initialize: (constant: Node?, requireds: Array, rest: Node?, posts: Array, opening_loc: Location?, closing_loc: Location?, location: Location) -> void



626
627
628
629
630
631
632
633
634
# File 'lib/prism/node.rb', line 626

def initialize(constant, requireds, rest, posts, opening_loc, closing_loc, location)
  @constant = constant
  @requireds = requireds
  @rest = rest
  @posts = posts
  @opening_loc = opening_loc
  @closing_loc = closing_loc
  @location = location
end

Instance Attribute Details

#closing_locObject (readonly)

attr_reader closing_loc: Location?



623
624
625
# File 'lib/prism/node.rb', line 623

def closing_loc
  @closing_loc
end

#constantObject (readonly)

attr_reader constant: Node?



608
609
610
# File 'lib/prism/node.rb', line 608

def constant
  @constant
end

#opening_locObject (readonly)

attr_reader opening_loc: Location?



620
621
622
# File 'lib/prism/node.rb', line 620

def opening_loc
  @opening_loc
end

#postsObject (readonly)

attr_reader posts: Array



617
618
619
# File 'lib/prism/node.rb', line 617

def posts
  @posts
end

#requiredsObject (readonly)

attr_reader requireds: Array



611
612
613
# File 'lib/prism/node.rb', line 611

def requireds
  @requireds
end

#restObject (readonly)

attr_reader rest: Node?



614
615
616
# File 'lib/prism/node.rb', line 614

def rest
  @rest
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



637
638
639
# File 'lib/prism/node.rb', line 637

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

#child_nodesObject Also known as: deconstruct

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



642
643
644
# File 'lib/prism/node.rb', line 642

def child_nodes
  [constant, *requireds, rest, *posts]
end

#closingObject

def closing: () -> String?



688
689
690
# File 'lib/prism/node.rb', line 688

def closing
  closing_loc&.slice
end

#comment_targetsObject

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



657
658
659
# File 'lib/prism/node.rb', line 657

def comment_targets
  [*constant, *requireds, *rest, *posts, *opening_loc, *closing_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



647
648
649
650
651
652
653
654
# File 'lib/prism/node.rb', line 647

def compact_child_nodes
  compact = []
  compact << constant if constant
  compact.concat(requireds)
  compact << rest if rest
  compact.concat(posts)
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> ArrayPatternNode



662
663
664
665
666
667
668
669
670
671
672
# File 'lib/prism/node.rb', line 662

def copy(**params)
  ArrayPatternNode.new(
    params.fetch(:constant) { constant },
    params.fetch(:requireds) { requireds },
    params.fetch(:rest) { rest },
    params.fetch(:posts) { posts },
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



678
679
680
# File 'lib/prism/node.rb', line 678

def deconstruct_keys(keys)
  { constant: constant, requireds: requireds, rest: rest, posts: posts, opening_loc: opening_loc, closing_loc: closing_loc, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'lib/prism/node.rb', line 692

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  if (constant = self.constant).nil?
    inspector << "├── constant: ∅\n"
  else
    inspector << "├── constant:\n"
    inspector << constant.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── requireds: #{inspector.list("#{inspector.prefix}", requireds)}"
  if (rest = self.rest).nil?
    inspector << "├── rest: ∅\n"
  else
    inspector << "├── rest:\n"
    inspector << rest.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── posts: #{inspector.list("#{inspector.prefix}", posts)}"
  inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n"
  inspector << "└── closing_loc: #{inspector.location(closing_loc)}\n"
  inspector.to_str
end

#openingObject

def opening: () -> String?



683
684
685
# File 'lib/prism/node.rb', line 683

def opening
  opening_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



727
728
729
# File 'lib/prism/node.rb', line 727

def type
  :array_pattern_node
end