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.



10612
10613
10614
10615
10616
# File 'lib/syntax_tree.rb', line 10612

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



10610
10611
10612
# File 'lib/syntax_tree.rb', line 10610

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



10607
10608
10609
# File 'lib/syntax_tree.rb', line 10607

def location
  @location
end

#variableObject (readonly)

Backref | VarRef

the variable being interpolated



10604
10605
10606
# File 'lib/syntax_tree.rb', line 10604

def variable
  @variable
end

Instance Method Details

#child_nodesObject



10618
10619
10620
# File 'lib/syntax_tree.rb', line 10618

def child_nodes
  [variable]
end

#format(q) ⇒ Object



10622
10623
10624
10625
10626
# File 'lib/syntax_tree.rb', line 10622

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

#pretty_print(q) ⇒ Object



10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
# File 'lib/syntax_tree.rb', line 10628

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



10639
10640
10641
10642
10643
10644
10645
10646
# File 'lib/syntax_tree.rb', line 10639

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