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.



11152
11153
11154
11155
11156
# File 'lib/syntax_tree.rb', line 11152

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



11150
11151
11152
# File 'lib/syntax_tree.rb', line 11150

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



11147
11148
11149
# File 'lib/syntax_tree.rb', line 11147

def location
  @location
end

#variableObject (readonly)

Backref | VarRef

the variable being interpolated



11144
11145
11146
# File 'lib/syntax_tree.rb', line 11144

def variable
  @variable
end

Instance Method Details

#child_nodesObject



11158
11159
11160
# File 'lib/syntax_tree.rb', line 11158

def child_nodes
  [variable]
end

#format(q) ⇒ Object



11162
11163
11164
11165
11166
# File 'lib/syntax_tree.rb', line 11162

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

#pretty_print(q) ⇒ Object



11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
# File 'lib/syntax_tree.rb', line 11168

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



11179
11180
11181
11182
11183
11184
11185
11186
# File 'lib/syntax_tree.rb', line 11179

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