class SyntaxTree::AryPtn
AryPtn
represents matching against an array pattern using the Ruby 2.7+ pattern matching syntax. It’s one of the more complicated nodes, because the four parameters that it accepts can almost all be nil.
case [1, 2, 3] in [Integer, Integer] "matched" in Container[Integer, Integer] "matched" in [Integer, *, Integer] "matched" end
An AryPtn
node is created with four parameters: an optional constant wrapper, an array of positional matches, an optional splat with identifier, and an optional array of positional matches that occur after the splat. All of the in clauses above would create an AryPtn
node.
Attributes
constant[R]
- nil |
VarRef
|ConstPathRef
-
the optional constant wrapper
posts[R]
- Array[
Node
] -
the list of positional arguments occurring after the
optional star if there is one
requireds[R]
- Array[
Node
] -
the regular positional arguments that this array
pattern is matching against
rest[R]
- nil |
VarField
-
the optional starred identifier that grabs up a list of
positional arguments
Public Class Methods
new(constant:, requireds:, rest:, posts:, location:)
click to toggle source
# File lib/syntax_tree/node.rb, line 1320 def initialize(constant:, requireds:, rest:, posts:, location:) @constant = constant @requireds = requireds @rest = rest @posts = posts @location = location @comments = [] end
Public Instance Methods
===(other)
click to toggle source
# File lib/syntax_tree/node.rb, line 1388 def ===(other) other.is_a?(AryPtn) && constant === other.constant && ArrayMatch.call(requireds, other.requireds) && rest === other.rest && ArrayMatch.call(posts, other.posts) end
accept(visitor)
click to toggle source
# File lib/syntax_tree/node.rb, line 1329 def accept(visitor) visitor.visit_aryptn(self) end
child_nodes()
click to toggle source
# File lib/syntax_tree/node.rb, line 1333 def child_nodes [constant, *requireds, rest, *posts] end
Also aliased as: deconstruct
copy( constant: nil, requireds: nil, rest: nil, posts: nil, location: nil )
click to toggle source
# File lib/syntax_tree/node.rb, line 1337 def copy( constant: nil, requireds: nil, rest: nil, posts: nil, location: nil ) node = AryPtn.new( constant: constant || self.constant, requireds: requireds || self.requireds, rest: rest || self.rest, posts: posts || self.posts, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end
deconstruct_keys(_keys)
click to toggle source
# File lib/syntax_tree/node.rb, line 1359 def deconstruct_keys(_keys) { constant: constant, requireds: requireds, rest: rest, posts: posts, location: location, comments: comments } end
format(q)
click to toggle source
# File lib/syntax_tree/node.rb, line 1370 def format(q) q.group do q.format(constant) if constant q.text("[") q.indent do q.breakable_empty parts = [*requireds] parts << RestFormatter.new(rest) if rest parts += posts q.seplist(parts) { |part| q.format(part) } end q.breakable_empty q.text("]") end end