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.



3735
3736
3737
3738
3739
# File 'lib/syntax_tree.rb', line 3735

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



3733
3734
3735
# File 'lib/syntax_tree.rb', line 3733

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



3730
3731
3732
# File 'lib/syntax_tree.rb', line 3730

def location
  @location
end

#valueObject (readonly)

String

the name of the constant



3727
3728
3729
# File 'lib/syntax_tree.rb', line 3727

def value
  @value
end

Instance Method Details

#child_nodesObject



3741
3742
3743
# File 'lib/syntax_tree.rb', line 3741

def child_nodes
  []
end

#format(q) ⇒ Object



3745
3746
3747
# File 'lib/syntax_tree.rb', line 3745

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

#pretty_print(q) ⇒ Object



3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
# File 'lib/syntax_tree.rb', line 3749

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



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

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