Class: Prism::ArrayPatternNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::ArrayPatternNode
- 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
-
#closing_loc ⇒ Object
readonly
attr_reader closing_loc: Location?.
-
#constant ⇒ Object
readonly
attr_reader constant: Node?.
-
#opening_loc ⇒ Object
readonly
attr_reader opening_loc: Location?.
-
#posts ⇒ Object
readonly
attr_reader posts: Array.
-
#requireds ⇒ Object
readonly
attr_reader requireds: Array.
-
#rest ⇒ Object
readonly
attr_reader rest: Node?.
Class Method Summary collapse
-
.type ⇒ Object
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.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#closing ⇒ Object
def closing: () -> String?.
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#copy(**params) ⇒ Object
def copy: (**params) -> ArrayPatternNode.
- #deconstruct_keys(keys) ⇒ Object
- #initialize(constant, requireds, rest, posts, opening_loc, closing_loc, location) ⇒ ArrayPatternNode constructor
-
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String.
-
#opening ⇒ Object
def opening: () -> String?.
-
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform.
Constructor Details
#initialize(constant, requireds, rest, posts, opening_loc, closing_loc, location) ⇒ ArrayPatternNode
723 724 725 726 727 728 729 730 731 |
# File 'lib/prism/node.rb', line 723 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_loc ⇒ Object (readonly)
attr_reader closing_loc: Location?
720 721 722 |
# File 'lib/prism/node.rb', line 720 def closing_loc @closing_loc end |
#constant ⇒ Object (readonly)
attr_reader constant: Node?
705 706 707 |
# File 'lib/prism/node.rb', line 705 def constant @constant end |
#opening_loc ⇒ Object (readonly)
attr_reader opening_loc: Location?
717 718 719 |
# File 'lib/prism/node.rb', line 717 def opening_loc @opening_loc end |
#posts ⇒ Object (readonly)
attr_reader posts: Array
714 715 716 |
# File 'lib/prism/node.rb', line 714 def posts @posts end |
#requireds ⇒ Object (readonly)
attr_reader requireds: Array
708 709 710 |
# File 'lib/prism/node.rb', line 708 def requireds @requireds end |
#rest ⇒ Object (readonly)
attr_reader rest: Node?
711 712 713 |
# File 'lib/prism/node.rb', line 711 def rest @rest end |
Class Method Details
.type ⇒ Object
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
835 836 837 |
# File 'lib/prism/node.rb', line 835 def self.type :array_pattern_node end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
734 735 736 |
# File 'lib/prism/node.rb', line 734 def accept(visitor) visitor.visit_array_pattern_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
739 740 741 |
# File 'lib/prism/node.rb', line 739 def child_nodes [constant, *requireds, rest, *posts] end |
#closing ⇒ Object
def closing: () -> String?
785 786 787 |
# File 'lib/prism/node.rb', line 785 def closing closing_loc&.slice end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
754 755 756 |
# File 'lib/prism/node.rb', line 754 def comment_targets [*constant, *requireds, *rest, *posts, *opening_loc, *closing_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
744 745 746 747 748 749 750 751 |
# File 'lib/prism/node.rb', line 744 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
759 760 761 762 763 764 765 766 767 768 769 |
# File 'lib/prism/node.rb', line 759 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
775 776 777 |
# File 'lib/prism/node.rb', line 775 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
def inspect(inspector: NodeInspector) -> String
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 |
# File 'lib/prism/node.rb', line 790 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 |
#opening ⇒ Object
def opening: () -> String?
780 781 782 |
# File 'lib/prism/node.rb', line 780 def opening opening_loc&.slice end |
#type ⇒ Object
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
825 826 827 |
# File 'lib/prism/node.rb', line 825 def type :array_pattern_node end |