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



11076
11077
11078
11079
11080
# File 'lib/syntax_tree.rb', line 11076

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



11074
11075
11076
# File 'lib/syntax_tree.rb', line 11074

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



11071
11072
11073
# File 'lib/syntax_tree.rb', line 11071

def location
  @location
end

#variableObject (readonly)

Backref | VarRef

the variable being interpolated



11068
11069
11070
# File 'lib/syntax_tree.rb', line 11068

def variable
  @variable
end

Instance Method Details

#child_nodesObject



11082
11083
11084
# File 'lib/syntax_tree.rb', line 11082

def child_nodes
  [variable]
end

#format(q) ⇒ Object



11086
11087
11088
11089
11090
# File 'lib/syntax_tree.rb', line 11086

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

#pretty_print(q) ⇒ Object



11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
# File 'lib/syntax_tree.rb', line 11092

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



11103
11104
11105
11106
11107
11108
11109
11110
# File 'lib/syntax_tree.rb', line 11103

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