Class: SyntaxTree::StringDVar

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

StringDVar represents shorthand interpolation of a variable into a string. It allows you to take an instance variable, class variable, or global variable and omit the braces when interpolating.

"#@variable"

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(variable:, location:) ⇒ StringDVar

Returns a new instance of StringDVar.



10149
10150
10151
10152
10153
# File 'lib/syntax_tree/node.rb', line 10149

def initialize(variable:, location:)
  @variable = variable
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10147
10148
10149
# File 'lib/syntax_tree/node.rb', line 10147

def comments
  @comments
end

#variableObject (readonly)

Backref | VarRef

the variable being interpolated



10144
10145
10146
# File 'lib/syntax_tree/node.rb', line 10144

def variable
  @variable
end

Instance Method Details

#===(other) ⇒ Object



10186
10187
10188
# File 'lib/syntax_tree/node.rb', line 10186

def ===(other)
  other.is_a?(StringDVar) && variable === other.variable
end

#accept(visitor) ⇒ Object



10155
10156
10157
# File 'lib/syntax_tree/node.rb', line 10155

def accept(visitor)
  visitor.visit_string_dvar(self)
end

#child_nodesObject Also known as: deconstruct



10159
10160
10161
# File 'lib/syntax_tree/node.rb', line 10159

def child_nodes
  [variable]
end

#copy(variable: nil, location: nil) ⇒ Object



10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
# File 'lib/syntax_tree/node.rb', line 10163

def copy(variable: nil, location: nil)
  node =
    StringDVar.new(
      variable: variable || self.variable,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



10176
10177
10178
# File 'lib/syntax_tree/node.rb', line 10176

def deconstruct_keys(_keys)
  { variable: variable, location: location, comments: comments }
end

#format(q) ⇒ Object



10180
10181
10182
10183
10184
# File 'lib/syntax_tree/node.rb', line 10180

def format(q)
  q.text('#{')
  q.format(variable)
  q.text("}")
end