Class: SyntaxTree::In
Overview
In represents using the in keyword within the Ruby 2.7+ pattern matching syntax.
case value
in pattern
end
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#consequent ⇒ Object
readonly
- nil | In | Else
-
the next clause in the chain.
-
#pattern ⇒ Object
readonly
- untyped
-
the pattern to check against.
-
#statements ⇒ Object
readonly
- Statements
-
the expressions to execute if the pattern matched.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(pattern: nil, statements: nil, consequent: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(pattern:, statements:, consequent:, location:) ⇒ In
constructor
A new instance of In.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(pattern:, statements:, consequent:, location:) ⇒ In
Returns a new instance of In.
6632 6633 6634 6635 6636 6637 6638 |
# File 'lib/syntax_tree/node.rb', line 6632 def initialize(pattern:, statements:, consequent:, location:) @pattern = pattern @statements = statements @consequent = consequent @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
6630 6631 6632 |
# File 'lib/syntax_tree/node.rb', line 6630 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | In | Else
-
the next clause in the chain
6627 6628 6629 |
# File 'lib/syntax_tree/node.rb', line 6627 def consequent @consequent end |
#pattern ⇒ Object (readonly)
- untyped
-
the pattern to check against
6621 6622 6623 |
# File 'lib/syntax_tree/node.rb', line 6621 def pattern @pattern end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to execute if the pattern matched
6624 6625 6626 |
# File 'lib/syntax_tree/node.rb', line 6624 def statements @statements end |
Instance Method Details
#===(other) ⇒ Object
6694 6695 6696 6697 |
# File 'lib/syntax_tree/node.rb', line 6694 def ===(other) other.is_a?(In) && pattern === other.pattern && statements === other.statements && consequent === other.consequent end |
#accept(visitor) ⇒ Object
6640 6641 6642 |
# File 'lib/syntax_tree/node.rb', line 6640 def accept(visitor) visitor.visit_in(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
6644 6645 6646 |
# File 'lib/syntax_tree/node.rb', line 6644 def child_nodes [pattern, statements, consequent] end |
#copy(pattern: nil, statements: nil, consequent: nil, location: nil) ⇒ Object
6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 |
# File 'lib/syntax_tree/node.rb', line 6648 def copy(pattern: nil, statements: nil, consequent: nil, location: nil) node = In.new( pattern: pattern || self.pattern, statements: statements || self.statements, consequent: consequent || self.consequent, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
6663 6664 6665 6666 6667 6668 6669 6670 6671 |
# File 'lib/syntax_tree/node.rb', line 6663 def deconstruct_keys(_keys) { pattern: pattern, statements: statements, consequent: consequent, location: location, comments: comments } end |
#format(q) ⇒ Object
6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 |
# File 'lib/syntax_tree/node.rb', line 6673 def format(q) keyword = "in " q.group do q.text(keyword) q.nest(keyword.length) { q.format(pattern) } unless statements.empty? q.indent do q.breakable_force q.format(statements) end end if consequent q.breakable_force q.format(consequent) end end end |