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
- Node
-
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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(pattern:, statements:, consequent:, location:) ⇒ In
Returns a new instance of In.
6747 6748 6749 6750 6751 6752 6753 |
# File 'lib/syntax_tree/node.rb', line 6747 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
6745 6746 6747 |
# File 'lib/syntax_tree/node.rb', line 6745 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | In | Else
-
the next clause in the chain
6742 6743 6744 |
# File 'lib/syntax_tree/node.rb', line 6742 def consequent @consequent end |
#pattern ⇒ Object (readonly)
- Node
-
the pattern to check against
6736 6737 6738 |
# File 'lib/syntax_tree/node.rb', line 6736 def pattern @pattern end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to execute if the pattern matched
6739 6740 6741 |
# File 'lib/syntax_tree/node.rb', line 6739 def statements @statements end |
Instance Method Details
#===(other) ⇒ Object
6812 6813 6814 6815 |
# File 'lib/syntax_tree/node.rb', line 6812 def ===(other) other.is_a?(In) && pattern === other.pattern && statements === other.statements && consequent === other.consequent end |
#accept(visitor) ⇒ Object
6755 6756 6757 |
# File 'lib/syntax_tree/node.rb', line 6755 def accept(visitor) visitor.visit_in(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
6759 6760 6761 |
# File 'lib/syntax_tree/node.rb', line 6759 def child_nodes [pattern, statements, consequent] end |
#copy(pattern: nil, statements: nil, consequent: nil, location: nil) ⇒ Object
6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 |
# File 'lib/syntax_tree/node.rb', line 6763 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
6778 6779 6780 6781 6782 6783 6784 6785 6786 |
# File 'lib/syntax_tree/node.rb', line 6778 def deconstruct_keys(_keys) { pattern: pattern, statements: statements, consequent: consequent, location: location, comments: comments } end |
#format(q) ⇒ Object
6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 |
# File 'lib/syntax_tree/node.rb', line 6788 def format(q) keyword = "in " pattern = self.pattern consequent = self.consequent q.group do q.text(keyword) q.nest(keyword.length) { q.format(pattern) } q.text(" then") if pattern.is_a?(RangeNode) && pattern.right.nil? 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 |