Class: SyntaxTree::Words
Overview
Words represents a string literal array with interpolation.
%W[one two three]
Instance Attribute Summary collapse
-
#beginning ⇒ Object
readonly
- WordsBeg
-
the token that opens this array literal.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#elements ⇒ Object
readonly
- Array[ Word ]
-
the elements of this array.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(beginning: nil, elements: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(beginning:, elements:, location:) ⇒ Words
constructor
A new instance of Words.
Methods inherited from Node
#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(beginning:, elements:, location:) ⇒ Words
Returns a new instance of Words.
12053 12054 12055 12056 12057 12058 |
# File 'lib/syntax_tree/node.rb', line 12053 def initialize(beginning:, elements:, location:) @beginning = beginning @elements = elements @location = location @comments = [] end |
Instance Attribute Details
#beginning ⇒ Object (readonly)
- WordsBeg
-
the token that opens this array literal
12045 12046 12047 |
# File 'lib/syntax_tree/node.rb', line 12045 def beginning @beginning end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
12051 12052 12053 |
# File 'lib/syntax_tree/node.rb', line 12051 def comments @comments end |
#elements ⇒ Object (readonly)
- Array[ Word ]
-
the elements of this array
12048 12049 12050 |
# File 'lib/syntax_tree/node.rb', line 12048 def elements @elements end |
Instance Method Details
#===(other) ⇒ Object
12109 12110 12111 12112 |
# File 'lib/syntax_tree/node.rb', line 12109 def ===(other) other.is_a?(Words) && beginning === other.beginning && ArrayMatch.call(elements, other.elements) end |
#accept(visitor) ⇒ Object
12060 12061 12062 |
# File 'lib/syntax_tree/node.rb', line 12060 def accept(visitor) visitor.visit_words(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
12064 12065 12066 |
# File 'lib/syntax_tree/node.rb', line 12064 def child_nodes [] end |
#copy(beginning: nil, elements: nil, location: nil) ⇒ Object
12068 12069 12070 12071 12072 12073 12074 |
# File 'lib/syntax_tree/node.rb', line 12068 def copy(beginning: nil, elements: nil, location: nil) Words.new( beginning: beginning || self.beginning, elements: elements || self.elements, location: location || self.location ) end |
#deconstruct_keys(_keys) ⇒ Object
12078 12079 12080 12081 12082 12083 12084 12085 |
# File 'lib/syntax_tree/node.rb', line 12078 def deconstruct_keys(_keys) { beginning: beginning, elements: elements, location: location, comments: comments } end |
#format(q) ⇒ Object
12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 |
# File 'lib/syntax_tree/node.rb', line 12087 def format(q) opening, closing = "%W[", "]" if elements.any? { |element| element.match?(/[\[\]]/) } opening = beginning.value closing = Quotes.matching(opening[2]) end q.text(opening) q.group do q.indent do q.breakable_empty q.seplist( elements, ArrayLiteral::BREAKABLE_SPACE_SEPARATOR ) { |element| q.format(element) } end q.breakable_empty end q.text(closing) end |