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

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(variable:, location:) ⇒ StringDVar

Returns a new instance of StringDVar.



10022
10023
10024
10025
10026
# File 'lib/syntax_tree/node.rb', line 10022

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10020
10021
10022
# File 'lib/syntax_tree/node.rb', line 10020

def comments
  @comments
end

#variableObject (readonly)

Backref | VarRef

the variable being interpolated



10017
10018
10019
# File 'lib/syntax_tree/node.rb', line 10017

def variable
  @variable
end

Instance Method Details

#===(other) ⇒ Object



10059
10060
10061
# File 'lib/syntax_tree/node.rb', line 10059

def ===(other)
  other.is_a?(StringDVar) && variable === other.variable
end

#accept(visitor) ⇒ Object



10028
10029
10030
# File 'lib/syntax_tree/node.rb', line 10028

def accept(visitor)
  visitor.visit_string_dvar(self)
end

#child_nodesObject Also known as: deconstruct



10032
10033
10034
# File 'lib/syntax_tree/node.rb', line 10032

def child_nodes
  [variable]
end

#copy(variable: nil, location: nil) ⇒ Object



10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
# File 'lib/syntax_tree/node.rb', line 10036

def copy(variable: nil, location: nil)
  node =
    StringDVar.new(
      variable: variable || self.variable,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



10049
10050
10051
# File 'lib/syntax_tree/node.rb', line 10049

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

#format(q) ⇒ Object



10053
10054
10055
10056
10057
# File 'lib/syntax_tree/node.rb', line 10053

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