Class: SyntaxTree::AryPtn
Overview
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.
Defined Under Namespace
Classes: RestFormatter
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#constant ⇒ Object
readonly
- nil | VarRef
-
the optional constant wrapper.
-
#posts ⇒ Object
readonly
- Array[ untyped ]
-
the list of positional arguments occurring after the optional star if there is one.
-
#requireds ⇒ Object
readonly
- Array[ untyped ]
-
the regular positional arguments that this array pattern is matching against.
-
#rest ⇒ Object
readonly
- nil | VarField
-
the optional starred identifier that grabs up a list of positional arguments.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(constant: nil, requireds: nil, rest: nil, posts: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(constant:, requireds:, rest:, posts:, location:) ⇒ AryPtn
constructor
A new instance of AryPtn.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(constant:, requireds:, rest:, posts:, location:) ⇒ AryPtn
Returns a new instance of AryPtn.
1351 1352 1353 1354 1355 1356 1357 1358 |
# File 'lib/syntax_tree/node.rb', line 1351 def initialize(constant:, requireds:, rest:, posts:, location:) @constant = constant @requireds = requireds @rest = rest @posts = posts @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
1349 1350 1351 |
# File 'lib/syntax_tree/node.rb', line 1349 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | VarRef
-
the optional constant wrapper
1334 1335 1336 |
# File 'lib/syntax_tree/node.rb', line 1334 def constant @constant end |
#posts ⇒ Object (readonly)
- Array[ untyped ]
-
the list of positional arguments occurring after the
optional star if there is one
1346 1347 1348 |
# File 'lib/syntax_tree/node.rb', line 1346 def posts @posts end |
#requireds ⇒ Object (readonly)
- Array[ untyped ]
-
the regular positional arguments that this array
pattern is matching against
1338 1339 1340 |
# File 'lib/syntax_tree/node.rb', line 1338 def requireds @requireds end |
#rest ⇒ Object (readonly)
- nil | VarField
-
the optional starred identifier that grabs up a list of
positional arguments
1342 1343 1344 |
# File 'lib/syntax_tree/node.rb', line 1342 def rest @rest end |
Instance Method Details
#===(other) ⇒ Object
1419 1420 1421 1422 1423 |
# File 'lib/syntax_tree/node.rb', line 1419 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) ⇒ Object
1360 1361 1362 |
# File 'lib/syntax_tree/node.rb', line 1360 def accept(visitor) visitor.visit_aryptn(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
1364 1365 1366 |
# File 'lib/syntax_tree/node.rb', line 1364 def child_nodes [constant, *requireds, rest, *posts] end |
#copy(constant: nil, requireds: nil, rest: nil, posts: nil, location: nil) ⇒ Object
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 |
# File 'lib/syntax_tree/node.rb', line 1368 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) ⇒ Object
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 |
# File 'lib/syntax_tree/node.rb', line 1390 def deconstruct_keys(_keys) { constant: constant, requireds: requireds, rest: rest, posts: posts, location: location, comments: comments } end |
#format(q) ⇒ Object
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 |
# File 'lib/syntax_tree/node.rb', line 1401 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 |