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.



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

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#variableObject (readonly)

Backref | VarRef

the variable being interpolated



10026
10027
10028
# File 'lib/syntax_tree/node.rb', line 10026

def variable
  @variable
end

Instance Method Details

#===(other) ⇒ Object



10068
10069
10070
# File 'lib/syntax_tree/node.rb', line 10068

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

#accept(visitor) ⇒ Object



10037
10038
10039
# File 'lib/syntax_tree/node.rb', line 10037

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

#child_nodesObject Also known as: deconstruct



10041
10042
10043
# File 'lib/syntax_tree/node.rb', line 10041

def child_nodes
  [variable]
end

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



10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
# File 'lib/syntax_tree/node.rb', line 10045

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



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

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

#format(q) ⇒ Object



10062
10063
10064
10065
10066
# File 'lib/syntax_tree/node.rb', line 10062

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