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[ Node ]
-
the list of positional arguments occurring after the optional star if there is one.
-
#requireds ⇒ Object
readonly
- Array[ Node ]
-
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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(constant:, requireds:, rest:, posts:, location:) ⇒ AryPtn
Returns a new instance of AryPtn.
1316 1317 1318 1319 1320 1321 1322 1323 |
# File 'lib/syntax_tree/node.rb', line 1316 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
1314 1315 1316 |
# File 'lib/syntax_tree/node.rb', line 1314 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | VarRef
-
the optional constant wrapper
1299 1300 1301 |
# File 'lib/syntax_tree/node.rb', line 1299 def constant @constant end |
#posts ⇒ Object (readonly)
- Array[ Node ]
-
the list of positional arguments occurring after the
optional star if there is one
1311 1312 1313 |
# File 'lib/syntax_tree/node.rb', line 1311 def posts @posts end |
#requireds ⇒ Object (readonly)
- Array[ Node ]
-
the regular positional arguments that this array
pattern is matching against
1303 1304 1305 |
# File 'lib/syntax_tree/node.rb', line 1303 def requireds @requireds end |
#rest ⇒ Object (readonly)
- nil | VarField
-
the optional starred identifier that grabs up a list of
positional arguments
1307 1308 1309 |
# File 'lib/syntax_tree/node.rb', line 1307 def rest @rest end |
Instance Method Details
#===(other) ⇒ Object
1384 1385 1386 1387 1388 |
# File 'lib/syntax_tree/node.rb', line 1384 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
1325 1326 1327 |
# File 'lib/syntax_tree/node.rb', line 1325 def accept(visitor) visitor.visit_aryptn(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
1329 1330 1331 |
# File 'lib/syntax_tree/node.rb', line 1329 def child_nodes [constant, *requireds, rest, *posts] end |
#copy(constant: nil, requireds: nil, rest: nil, posts: nil, location: nil) ⇒ Object
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 |
# File 'lib/syntax_tree/node.rb', line 1333 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
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 |
# File 'lib/syntax_tree/node.rb', line 1355 def deconstruct_keys(_keys) { constant: constant, requireds: requireds, rest: rest, posts: posts, location: location, comments: comments } end |
#format(q) ⇒ Object
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 |
# File 'lib/syntax_tree/node.rb', line 1366 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 |