Class: SyntaxTree::DynaSymbol
Overview
DynaSymbol represents a symbol literal that uses quotes to dynamically define its value.
:"#{variable}"
They can also be used as a special kind of dynamic hash key, as in:
{ "#{key}": value }
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#parts ⇒ Object
readonly
- Array[ StringDVar | StringEmbExpr | TStringContent ]
-
the parts of the dynamic symbol.
-
#quote ⇒ Object
readonly
- String
-
the quote used to delimit the dynamic symbol.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(parts: nil, quote: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(parts:, quote:, location:) ⇒ DynaSymbol
constructor
A new instance of DynaSymbol.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(parts:, quote:, location:) ⇒ DynaSymbol
Returns a new instance of DynaSymbol.
4587 4588 4589 4590 4591 4592 |
# File 'lib/syntax_tree/node.rb', line 4587 def initialize(parts:, quote:, location:) @parts = parts @quote = quote @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
4585 4586 4587 |
# File 'lib/syntax_tree/node.rb', line 4585 def comments @comments end |
#parts ⇒ Object (readonly)
- Array[ StringDVar | StringEmbExpr | TStringContent ]
-
the parts of the
dynamic symbol
4579 4580 4581 |
# File 'lib/syntax_tree/node.rb', line 4579 def parts @parts end |
#quote ⇒ Object (readonly)
- String
-
the quote used to delimit the dynamic symbol
4582 4583 4584 |
# File 'lib/syntax_tree/node.rb', line 4582 def quote @quote end |
Instance Method Details
#===(other) ⇒ Object
4649 4650 4651 4652 |
# File 'lib/syntax_tree/node.rb', line 4649 def ===(other) other.is_a?(DynaSymbol) && ArrayMatch.call(parts, other.parts) && quote === other.quote end |
#accept(visitor) ⇒ Object
4594 4595 4596 |
# File 'lib/syntax_tree/node.rb', line 4594 def accept(visitor) visitor.visit_dyna_symbol(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
4598 4599 4600 |
# File 'lib/syntax_tree/node.rb', line 4598 def child_nodes parts end |
#copy(parts: nil, quote: nil, location: nil) ⇒ Object
4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 |
# File 'lib/syntax_tree/node.rb', line 4602 def copy(parts: nil, quote: nil, location: nil) node = DynaSymbol.new( parts: parts || self.parts, quote: quote || self.quote, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
4616 4617 4618 |
# File 'lib/syntax_tree/node.rb', line 4616 def deconstruct_keys(_keys) { parts: parts, quote: quote, location: location, comments: comments } end |
#format(q) ⇒ Object
4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 |
# File 'lib/syntax_tree/node.rb', line 4620 def format(q) opening_quote, closing_quote = quotes(q) q.text(opening_quote) q.group do parts.each do |part| if part.is_a?(TStringContent) value = Quotes.normalize(part.value, closing_quote) first = true value.each_line(chomp: true) do |line| if first first = false else q.breakable_return end q.text(line) end q.breakable_return if value.end_with?("\n") else q.format(part) end end end q.text(closing_quote) end |