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.



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

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10178
10179
10180
# File 'lib/syntax_tree/node.rb', line 10178

def comments
  @comments
end

#variableObject (readonly)

Backref | VarRef

the variable being interpolated



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

def variable
  @variable
end

Instance Method Details

#===(other) ⇒ Object



10217
10218
10219
# File 'lib/syntax_tree/node.rb', line 10217

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



10190
10191
10192
# File 'lib/syntax_tree/node.rb', line 10190

def child_nodes
  [variable]
end

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



10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
# File 'lib/syntax_tree/node.rb', line 10194

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



10207
10208
10209
# File 'lib/syntax_tree/node.rb', line 10207

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

#format(q) ⇒ Object



10211
10212
10213
10214
10215
# File 'lib/syntax_tree/node.rb', line 10211

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