Class: SyntaxTree::Const

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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

Instance Method Summary collapse

Constructor Details

#initialize(value:, location:, comments: []) ⇒ Const

Returns a new instance of Const.



4034
4035
4036
4037
4038
# File 'lib/syntax_tree.rb', line 4034

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4032
4033
4034
# File 'lib/syntax_tree.rb', line 4032

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4029
4030
4031
# File 'lib/syntax_tree.rb', line 4029

def location
  @location
end

#valueObject (readonly)

String

the name of the constant



4026
4027
4028
# File 'lib/syntax_tree.rb', line 4026

def value
  @value
end

Instance Method Details

#child_nodesObject



4040
4041
4042
# File 'lib/syntax_tree.rb', line 4040

def child_nodes
  []
end

#format(q) ⇒ Object



4044
4045
4046
# File 'lib/syntax_tree.rb', line 4044

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

#pretty_print(q) ⇒ Object



4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
# File 'lib/syntax_tree.rb', line 4048

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("const")

    q.breakable
    q.pp(value)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



4059
4060
4061
4062
4063
# File 'lib/syntax_tree.rb', line 4059

def to_json(*opts)
  { type: :const, value: value, loc: location, cmts: comments }.to_json(
    *opts
  )
end