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.
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
-
#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
5326 5327 5328 5329 5330 5331 5332 5333 5334 |
# File 'lib/prism/node.rb', line 5326 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?
5323 5324 5325 |
# File 'lib/prism/node.rb', line 5323 def closing_loc @closing_loc end |
#constant ⇒ Object (readonly)
attr_reader constant: Node?
5308 5309 5310 |
# File 'lib/prism/node.rb', line 5308 def constant @constant end |
#left ⇒ Object (readonly)
attr_reader left: Node
5311 5312 5313 |
# File 'lib/prism/node.rb', line 5311 def left @left end |
#opening_loc ⇒ Object (readonly)
attr_reader opening_loc: Location?
5320 5321 5322 |
# File 'lib/prism/node.rb', line 5320 def opening_loc @opening_loc end |
#requireds ⇒ Object (readonly)
attr_reader requireds: Array
5314 5315 5316 |
# File 'lib/prism/node.rb', line 5314 def requireds @requireds end |
#right ⇒ Object (readonly)
attr_reader right: Node
5317 5318 5319 |
# File 'lib/prism/node.rb', line 5317 def right @right end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
5337 5338 5339 |
# File 'lib/prism/node.rb', line 5337 def accept(visitor) visitor.visit_find_pattern_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
5342 5343 5344 |
# File 'lib/prism/node.rb', line 5342 def child_nodes [constant, left, *requireds, right] end |
#closing ⇒ Object
def closing: () -> String?
5388 5389 5390 |
# File 'lib/prism/node.rb', line 5388 def closing closing_loc&.slice end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
5357 5358 5359 |
# File 'lib/prism/node.rb', line 5357 def comment_targets [*constant, left, *requireds, right, *opening_loc, *closing_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
5347 5348 5349 5350 5351 5352 5353 5354 |
# File 'lib/prism/node.rb', line 5347 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
5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 |
# File 'lib/prism/node.rb', line 5362 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
5378 5379 5380 |
# File 'lib/prism/node.rb', line 5378 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
5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 |
# File 'lib/prism/node.rb', line 5392 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?
5383 5384 5385 |
# File 'lib/prism/node.rb', line 5383 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
5424 5425 5426 |
# File 'lib/prism/node.rb', line 5424 def type :find_pattern_node end |