Class: SyntaxTree::Const

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Const represents a literal value that looks like a constant. This could actually be a reference to a constant:

Constant

It could also be something that looks like a constant in another context, as in a method call to a capitalized method:

object.Constant

or a symbol that starts with a capital letter:

:Constant

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(value:, location:) ⇒ Const

Returns a new instance of Const.



3763
3764
3765
3766
3767
# File 'lib/syntax_tree/node.rb', line 3763

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3761
3762
3763
# File 'lib/syntax_tree/node.rb', line 3761

def comments
  @comments
end

#valueObject (readonly)

String

the name of the constant



3758
3759
3760
# File 'lib/syntax_tree/node.rb', line 3758

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



3798
3799
3800
# File 'lib/syntax_tree/node.rb', line 3798

def ===(other)
  other.is_a?(Const) && value === other.value
end

#accept(visitor) ⇒ Object



3769
3770
3771
# File 'lib/syntax_tree/node.rb', line 3769

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

#child_nodesObject Also known as: deconstruct



3773
3774
3775
# File 'lib/syntax_tree/node.rb', line 3773

def child_nodes
  []
end

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



3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
# File 'lib/syntax_tree/node.rb', line 3777

def copy(value: nil, location: nil)
  node =
    Const.new(
      value: value || self.value,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



3790
3791
3792
# File 'lib/syntax_tree/node.rb', line 3790

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

#format(q) ⇒ Object



3794
3795
3796
# File 'lib/syntax_tree/node.rb', line 3794

def format(q)
  q.text(value)
end