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



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

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10210
10211
10212
# File 'lib/syntax_tree/node.rb', line 10210

def comments
  @comments
end

#variableObject (readonly)

Backref | VarRef

the variable being interpolated



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

def variable
  @variable
end

Instance Method Details

#===(other) ⇒ Object



10249
10250
10251
# File 'lib/syntax_tree/node.rb', line 10249

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



10222
10223
10224
# File 'lib/syntax_tree/node.rb', line 10222

def child_nodes
  [variable]
end

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



10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
# File 'lib/syntax_tree/node.rb', line 10226

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



10239
10240
10241
# File 'lib/syntax_tree/node.rb', line 10239

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

#format(q) ⇒ Object



10243
10244
10245
10246
10247
# File 'lib/syntax_tree/node.rb', line 10243

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