Class: SyntaxTree::FndPtn
Overview
FndPtn represents matching against a pattern where you find a pattern in an array using the Ruby 3.0+ pattern matching syntax.
case value
in [*, 7, *]
end
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#constant ⇒ Object
readonly
- nil | VarRef | ConstPathRef
-
the optional constant wrapper.
-
#left ⇒ Object
readonly
- VarField
-
the splat on the left-hand side.
-
#right ⇒ Object
readonly
- VarField
-
the splat on the right-hand side.
-
#values ⇒ Object
readonly
- Array[ Node ]
-
the list of positional expressions in the pattern that are being matched.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(constant: nil, left: nil, values: nil, right: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(constant:, left:, values:, right:, location:) ⇒ FndPtn
constructor
A new instance of FndPtn.
Methods inherited from Node
#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(constant:, left:, values:, right:, location:) ⇒ FndPtn
Returns a new instance of FndPtn.
5450 5451 5452 5453 5454 5455 5456 5457 |
# File 'lib/syntax_tree/node.rb', line 5450 def initialize(constant:, left:, values:, right:, location:) @constant = constant @left = left @values = values @right = right @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
5448 5449 5450 |
# File 'lib/syntax_tree/node.rb', line 5448 def comments @comments end |
#constant ⇒ Object (readonly)
- nil | VarRef | ConstPathRef
-
the optional constant wrapper
5435 5436 5437 |
# File 'lib/syntax_tree/node.rb', line 5435 def constant @constant end |
#left ⇒ Object (readonly)
- VarField
-
the splat on the left-hand side
5438 5439 5440 |
# File 'lib/syntax_tree/node.rb', line 5438 def left @left end |
#right ⇒ Object (readonly)
- VarField
-
the splat on the right-hand side
5445 5446 5447 |
# File 'lib/syntax_tree/node.rb', line 5445 def right @right end |
#values ⇒ Object (readonly)
- Array[ Node ]
-
the list of positional expressions in the pattern that
are being matched
5442 5443 5444 |
# File 'lib/syntax_tree/node.rb', line 5442 def values @values end |
Instance Method Details
#===(other) ⇒ Object
5519 5520 5521 5522 5523 |
# File 'lib/syntax_tree/node.rb', line 5519 def ===(other) other.is_a?(FndPtn) && constant === other.constant && left === other.left && ArrayMatch.call(values, other.values) && right === other.right end |
#accept(visitor) ⇒ Object
5459 5460 5461 |
# File 'lib/syntax_tree/node.rb', line 5459 def accept(visitor) visitor.visit_fndptn(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
5463 5464 5465 |
# File 'lib/syntax_tree/node.rb', line 5463 def child_nodes [constant, left, *values, right] end |
#copy(constant: nil, left: nil, values: nil, right: nil, location: nil) ⇒ Object
5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 |
# File 'lib/syntax_tree/node.rb', line 5467 def copy(constant: nil, left: nil, values: nil, right: nil, location: nil) node = FndPtn.new( constant: constant || self.constant, left: left || self.left, values: values || self.values, right: right || self.right, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 |
# File 'lib/syntax_tree/node.rb', line 5483 def deconstruct_keys(_keys) { constant: constant, left: left, values: values, right: right, location: location, comments: comments } end |
#format(q) ⇒ Object
5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 |
# File 'lib/syntax_tree/node.rb', line 5494 def format(q) q.format(constant) if constant q.group do q.text("[") q.indent do q.breakable_empty q.text("*") q.format(left) q.comma_breakable q.seplist(values) { |value| q.format(value) } q.comma_breakable q.text("*") q.format(right) end q.breakable_empty q.text("]") end end |