Class: SyntaxTree::StringDVar

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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

Instance Method Summary collapse

Constructor Details

#initialize(variable:, location:, comments: []) ⇒ StringDVar

Returns a new instance of StringDVar.



11284
11285
11286
11287
11288
# File 'lib/syntax_tree.rb', line 11284

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11282
11283
11284
# File 'lib/syntax_tree.rb', line 11282

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



11279
11280
11281
# File 'lib/syntax_tree.rb', line 11279

def location
  @location
end

#variableObject (readonly)

Backref | VarRef

the variable being interpolated



11276
11277
11278
# File 'lib/syntax_tree.rb', line 11276

def variable
  @variable
end

Instance Method Details

#child_nodesObject



11290
11291
11292
# File 'lib/syntax_tree.rb', line 11290

def child_nodes
  [variable]
end

#format(q) ⇒ Object



11294
11295
11296
11297
11298
# File 'lib/syntax_tree.rb', line 11294

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

#pretty_print(q) ⇒ Object



11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
# File 'lib/syntax_tree.rb', line 11300

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("string_dvar")

    q.breakable
    q.pp(variable)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



11311
11312
11313
11314
11315
11316
11317
11318
# File 'lib/syntax_tree.rb', line 11311

def to_json(*opts)
  {
    type: :string_dvar,
    var: variable,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end