class RuboCop::AST::NodePattern
- With no block and no captures: #match returns ‘true`.
- With no block, but multiple captures: captures are returned as an array.
- With no block, but one capture: the capture is returned.
value of the block through.
- With block: #match yields the captures (if any) and passes the return
from a matching AST.)
whether the pattern contained any “captures” (values which are extracted
return value depends on whether a block was provided to `#match`, and
If the match fails, `nil` will be returned. If the match succeeds, the
macros in `NodePattern::Macros` to define your own pattern-matching method.
pass an AST node to `NodePattern#match`. Alternatively, use one of the class
Initialize a new `NodePattern` with `NodePattern.new(pattern_string)`, then
Detailed syntax: /docs/modules/ROOT/pages/node_pattern.adoc
This class performs a pattern-matching operation on an AST node.
def self.descend(element, &block)
Yields its argument and any descendants, depth-first.
def self.descend(element, &block) return to_enum(__method__, element) unless block yield element if element.is_a?(::RuboCop::AST::Node) element.children.each do |child| descend(child, &block) end end nil end
def ==(other)
def ==(other) other.is_a?(NodePattern) && other.ast == ast end
def as_json(_options = nil) #:nodoc:
def as_json(_options = nil) #:nodoc: pattern end
def encode_with(coder) #:nodoc:
def encode_with(coder) #:nodoc: coder['pattern'] = pattern end
def freeze
def freeze @match_code.freeze @compiler.freeze super end
def init_with(coder) #:nodoc:
def init_with(coder) #:nodoc: initialize(coder['pattern']) end
def initialize(str, compiler: Compiler.new)
def initialize(str, compiler: Compiler.new) @pattern = str @ast = compiler.parser.parse(str) @compiler = compiler @match_code = @compiler.compile_as_node_pattern(@ast, var: VAR) @cache = {} end
def marshal_dump #:nodoc:
def marshal_dump #:nodoc: pattern end
def marshal_load(pattern) #:nodoc:
def marshal_load(pattern) #:nodoc: initialize pattern end
def match(*args, **rest, &block)
def match(*args, **rest, &block) @cache[:lambda] ||= as_lambda @cache[:lambda].call(*args, block: block, **rest) end
def to_s
def to_s "#<#{self.class} #{pattern}>" end