class Prism::Pattern
A pattern is an object that wraps a Ruby pattern matching expression. The expression would normally be passed to an in clause within a case expression or a rightward assignment expression. For example, in the following snippet:
case node in ConstantPathNode[ConstantReadNode[name: :Prism], ConstantReadNode[name: :Pattern]] end
the pattern is the ConstantPathNode[...] expression.
The pattern gets compiled into an object that responds to #call by running the compile method. This method itself will run back through Prism to parse the expression into a tree, then walk the tree to generate the necessary callable objects. For example, if you wanted to compile the expression above into a callable, you would:
callable = Prism::Pattern.new("ConstantPathNode[ConstantReadNode[name: :Prism], ConstantReadNode[name: :Pattern]]").compile callable.call(node)
The callable object returned by compile is guaranteed to respond to #call with a single argument, which is the node to match against. It also is guaranteed to respond to #===, which means it itself can be used in a case expression, as in:
case node when callable end
If the query given to the initializer cannot be compiled into a valid matcher (either because of a syntax error or because it is using syntax we do not yet support) then a Prism::Pattern::CompilationError will be raised.
A pattern is an object that wraps a Ruby pattern matching expression. The expression would normally be passed to an in clause within a case expression or a rightward assignment expression. For example, in the following snippet:
case node in ConstantPathNode[ConstantReadNode[name: :Prism], ConstantReadNode[name: :Pattern]] end
the pattern is the ConstantPathNode[...] expression.
The pattern gets compiled into an object that responds to #call by running the compile method. This method itself will run back through Prism to parse the expression into a tree, then walk the tree to generate the necessary callable objects. For example, if you wanted to compile the expression above into a callable, you would:
callable = Prism::Pattern.new("ConstantPathNode[ConstantReadNode[name: :Prism], ConstantReadNode[name: :Pattern]]").compile callable.call(node)
The callable object returned by compile is guaranteed to respond to #call with a single argument, which is the node to match against. It also is guaranteed to respond to #===, which means it itself can be used in a case expression, as in:
case node when callable end
If the query given to the initializer cannot be compiled into a valid matcher (either because of a syntax error or because it is using syntax we do not yet support) then a Prism::Pattern::CompilationError will be raised.
Attributes
Public Class Methods
(String query) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/pattern.rb, line 71 def initialize(query) @query = query @compiled = nil end
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 81
def combine_and: (Proc left, Proc right) -> Proc
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 87
def combine_or: (Proc left, Proc right) -> Proc
() → Proc
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/pattern.rb, line 80 def compile result = Prism.parse("case nil\nin #{query}\nend") case_match_node = result.value.statements.body.last raise CompilationError, case_match_node.inspect unless case_match_node.is_a?(CaseMatchNode) in_node = case_match_node.conditions.last raise CompilationError, in_node.inspect unless in_node.is_a?(InNode) compile_node(in_node.pattern) end
Compile the query into a callable object that can be used to match against nodes.
Compile the query into a callable object that can be used to match against nodes.
: () -> Proc
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 104
def compile_alternation_pattern_node: (AlternationPatternNode node) -> Proc
in foo | bar
: (AlternationPatternNode node) -> Proc
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 99
def compile_array_pattern_node: (ArrayPatternNode node) -> Proc
in [foo, bar, baz]
: (ArrayPatternNode node) -> Proc
(ConstantPathNode | ConstantReadNode node, Symbol name) → Proc
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 120
def compile_constant_name: (ConstantPathNode | ConstantReadNode node, Symbol name) -> Proc
Compile a name associated with a constant.
: ((ConstantPathNode | ConstantReadNode) node, Symbol name) -> Proc
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 109
def compile_constant_path_node: (ConstantPathNode node) -> Proc
: (ConstantPathNode node) -> Proc
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 115
def compile_constant_read_node: (ConstantReadNode node) -> Proc
in ConstantReadNode in String
: (ConstantReadNode node) -> Proc
(node node) → bot
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 94
def compile_error: (node node) -> bot
Raise an error because the given node is not supported. Note purposefully not typing this method since it is a no return method that Steep does not understand.
: (node node) -> bot
(HashPatternNode node) → Proc
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 126
def compile_hash_pattern_node: (HashPatternNode node) -> Proc
in InstanceVariableReadNode[name: Symbol] in { name: Symbol }
: (HashPatternNode node) -> Proc
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 131
def compile_nil_node: (NilNode node) -> Proc
(node node) → Proc
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 154
def compile_node: (node node) -> Proc
Compile any kind of node. Dispatch out to the individual compilation methods based on the type of node.
: (node node) -> Proc
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 136
def compile_regular_expression_node: (RegularExpressionNode node) -> Proc
in /foo/
: (RegularExpressionNode node) -> Proc
(StringNode node) → Proc
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 142
def compile_string_node: (StringNode node) -> Proc
in “” in “foo”
: (StringNode node) -> Proc
(SymbolNode node) → Proc
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/sig/generated/prism/pattern.rbs, line 148
def compile_symbol_node: (SymbolNode node) -> Proc
in :+ in :foo
: (SymbolNode node) -> Proc
(node root) → Enumerator[node, void]
(node root) { (node) → void } → void
Source
# File vendor/bundle/ruby/4.0.0/gems/prism-2.1.0-x86_64-linux-gnu/lib/prism/pattern.rb, line 99 def scan(root, &blk) return to_enum(:scan, root) unless block_given? @compiled ||= compile queue = [root] while (node = queue.shift) yield node if @compiled.call(node) # steep:ignore queue.concat(node.compact_child_nodes) end end
Scan the given node and all of its children for nodes that match the pattern. If a block is given, it will be called with each node that matches the pattern. If no block is given, an enumerator will be returned that will yield each node that matches the pattern.
Scan the given node and all of its children for nodes that match the pattern. If a block is given, it will be called with each node that matches the pattern. If no block is given, an enumerator will be returned that will yield each node that matches the pattern.
: (node root) -> Enumerator[node, void] : (node root) { (node) -> void } -> void