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.



10197
10198
10199
10200
10201
# File 'lib/syntax_tree/node.rb', line 10197

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10195
10196
10197
# File 'lib/syntax_tree/node.rb', line 10195

def comments
  @comments
end

#variableObject (readonly)

Backref | VarRef

the variable being interpolated



10192
10193
10194
# File 'lib/syntax_tree/node.rb', line 10192

def variable
  @variable
end

Instance Method Details

#===(other) ⇒ Object



10234
10235
10236
# File 'lib/syntax_tree/node.rb', line 10234

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

#accept(visitor) ⇒ Object



10203
10204
10205
# File 'lib/syntax_tree/node.rb', line 10203

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [variable]
end

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



10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
# File 'lib/syntax_tree/node.rb', line 10211

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



10224
10225
10226
# File 'lib/syntax_tree/node.rb', line 10224

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

#format(q) ⇒ Object



10228
10229
10230
10231
10232
# File 'lib/syntax_tree/node.rb', line 10228

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