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.
1305 1306 1307 1308 1309 1310 1311 1312 |
# File 'lib/syntax_tree/node.rb', line 1305 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
1303 1304 1305 |
# File 'lib/syntax_tree/node.rb', line 1303 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | VarRef
-
the optional constant wrapper
1288 1289 1290 |
# File 'lib/syntax_tree/node.rb', line 1288 def constant @constant end |
#posts ⇒ Object (readonly)
- Array[ untyped ]
-
the list of positional arguments occurring after the
optional star if there is one
1300 1301 1302 |
# File 'lib/syntax_tree/node.rb', line 1300 def posts @posts end |
#requireds ⇒ Object (readonly)
- Array[ untyped ]
-
the regular positional arguments that this array
pattern is matching against
1292 1293 1294 |
# File 'lib/syntax_tree/node.rb', line 1292 def requireds @requireds end |
#rest ⇒ Object (readonly)
- nil | VarField
-
the optional starred identifier that grabs up a list of
positional arguments
1296 1297 1298 |
# File 'lib/syntax_tree/node.rb', line 1296 def rest @rest end |
Instance Method Details
#===(other) ⇒ Object
1373 1374 1375 1376 1377 |
# File 'lib/syntax_tree/node.rb', line 1373 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
1314 1315 1316 |
# File 'lib/syntax_tree/node.rb', line 1314 def accept(visitor) visitor.visit_aryptn(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
1318 1319 1320 |
# File 'lib/syntax_tree/node.rb', line 1318 def child_nodes [constant, *requireds, rest, *posts] end |
#copy(constant: nil, requireds: nil, rest: nil, posts: nil, location: nil) ⇒ Object
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 |
# File 'lib/syntax_tree/node.rb', line 1322 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
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 |
# File 'lib/syntax_tree/node.rb', line 1344 def deconstruct_keys(_keys) { constant: constant, requireds: requireds, rest: rest, posts: posts, location: location, comments: comments } end |
#format(q) ⇒ Object
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 |
# File 'lib/syntax_tree/node.rb', line 1355 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 |