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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of StringDVar.



9630
9631
9632
9633
9634
# File 'lib/syntax_tree/node.rb', line 9630

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



9628
9629
9630
# File 'lib/syntax_tree/node.rb', line 9628

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



9625
9626
9627
# File 'lib/syntax_tree/node.rb', line 9625

def location
  @location
end

#variableObject (readonly)

Backref | VarRef

the variable being interpolated



9622
9623
9624
# File 'lib/syntax_tree/node.rb', line 9622

def variable
  @variable
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



9636
9637
9638
# File 'lib/syntax_tree/node.rb', line 9636

def child_nodes
  [variable]
end

#deconstruct_keys(keys) ⇒ Object



9642
9643
9644
# File 'lib/syntax_tree/node.rb', line 9642

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

#format(q) ⇒ Object



9646
9647
9648
9649
9650
# File 'lib/syntax_tree/node.rb', line 9646

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

#pretty_print(q) ⇒ Object



9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
# File 'lib/syntax_tree/node.rb', line 9652

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



9663
9664
9665
9666
9667
9668
9669
9670
# File 'lib/syntax_tree/node.rb', line 9663

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