Class: Prism::FindPatternNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::FindPatternNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents a find pattern in pattern matching.
foo in *bar, baz, *qux
^^^^^^^^^^^^^^^
foo in [*bar, baz, *qux]
^^^^^^^^^^^^^^^^^
foo in Foo(*bar, baz, *qux)
^^^^^^^^^^^^^^^^^^^^
Instance Attribute Summary collapse
-
#closing_loc ⇒ Object
readonly
attr_reader closing_loc: Location?.
-
#constant ⇒ Object
readonly
attr_reader constant: Node?.
-
#left ⇒ Object
readonly
attr_reader left: Node.
-
#opening_loc ⇒ Object
readonly
attr_reader opening_loc: Location?.
-
#requireds ⇒ Object
readonly
attr_reader requireds: Array.
-
#right ⇒ Object
readonly
attr_reader right: 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) -> FindPatternNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(constant, left, requireds, right, opening_loc, closing_loc, location) ⇒ FindPatternNode
constructor
def initialize: (constant: Node?, left: Node, requireds: Array, right: Node, opening_loc: Location?, closing_loc: Location?, location: Location) -> void.
-
#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, left, requireds, right, opening_loc, closing_loc, location) ⇒ FindPatternNode
def initialize: (constant: Node?, left: Node, requireds: Array, right: Node, opening_loc: Location?, closing_loc: Location?, location: Location) -> void
6117 6118 6119 6120 6121 6122 6123 6124 6125 |
# File 'lib/prism/node.rb', line 6117 def initialize(constant, left, requireds, right, opening_loc, closing_loc, location) @constant = constant @left = left @requireds = requireds @right = right @opening_loc = opening_loc @closing_loc = closing_loc @location = location end |
Instance Attribute Details
#closing_loc ⇒ Object (readonly)
attr_reader closing_loc: Location?
6114 6115 6116 |
# File 'lib/prism/node.rb', line 6114 def closing_loc @closing_loc end |
#constant ⇒ Object (readonly)
attr_reader constant: Node?
6099 6100 6101 |
# File 'lib/prism/node.rb', line 6099 def constant @constant end |
#left ⇒ Object (readonly)
attr_reader left: Node
6102 6103 6104 |
# File 'lib/prism/node.rb', line 6102 def left @left end |
#opening_loc ⇒ Object (readonly)
attr_reader opening_loc: Location?
6111 6112 6113 |
# File 'lib/prism/node.rb', line 6111 def opening_loc @opening_loc end |
#requireds ⇒ Object (readonly)
attr_reader requireds: Array
6105 6106 6107 |
# File 'lib/prism/node.rb', line 6105 def requireds @requireds end |
#right ⇒ Object (readonly)
attr_reader right: Node
6108 6109 6110 |
# File 'lib/prism/node.rb', line 6108 def right @right 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
6226 6227 6228 |
# File 'lib/prism/node.rb', line 6226 def self.type :find_pattern_node end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
6128 6129 6130 |
# File 'lib/prism/node.rb', line 6128 def accept(visitor) visitor.visit_find_pattern_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
6133 6134 6135 |
# File 'lib/prism/node.rb', line 6133 def child_nodes [constant, left, *requireds, right] end |
#closing ⇒ Object
def closing: () -> String?
6179 6180 6181 |
# File 'lib/prism/node.rb', line 6179 def closing closing_loc&.slice end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
6148 6149 6150 |
# File 'lib/prism/node.rb', line 6148 def comment_targets [*constant, left, *requireds, right, *opening_loc, *closing_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
6138 6139 6140 6141 6142 6143 6144 6145 |
# File 'lib/prism/node.rb', line 6138 def compact_child_nodes compact = [] compact << constant if constant compact << left compact.concat(requireds) compact << right compact end |
#copy(**params) ⇒ Object
def copy: (**params) -> FindPatternNode
6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 |
# File 'lib/prism/node.rb', line 6153 def copy(**params) FindPatternNode.new( params.fetch(:constant) { constant }, params.fetch(:left) { left }, params.fetch(:requireds) { requireds }, params.fetch(:right) { right }, params.fetch(:opening_loc) { opening_loc }, params.fetch(:closing_loc) { closing_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
6169 6170 6171 |
# File 'lib/prism/node.rb', line 6169 def deconstruct_keys(keys) { constant: constant, left: left, requireds: requireds, right: right, opening_loc: opening_loc, closing_loc: closing_loc, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String
6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 |
# File 'lib/prism/node.rb', line 6184 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 << "├── left:\n" inspector << inspector.child_node(left, "│ ") inspector << "├── requireds: #{inspector.list("#{inspector.prefix}│ ", requireds)}" inspector << "├── right:\n" inspector << inspector.child_node(right, "│ ") 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?
6174 6175 6176 |
# File 'lib/prism/node.rb', line 6174 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
6216 6217 6218 |
# File 'lib/prism/node.rb', line 6216 def type :find_pattern_node end |