Class: SyntaxTree::SClass

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

Overview

SClass represents a block of statements that should be evaluated within the context of the singleton class of an object. It’s frequently used to define singleton methods.

class << self
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target:, bodystmt:, location:, comments: []) ⇒ SClass

Returns a new instance of SClass.



10745
10746
10747
10748
10749
10750
# File 'lib/syntax_tree.rb', line 10745

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

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed



10737
10738
10739
# File 'lib/syntax_tree.rb', line 10737

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10743
10744
10745
# File 'lib/syntax_tree.rb', line 10743

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



10740
10741
10742
# File 'lib/syntax_tree.rb', line 10740

def location
  @location
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



10734
10735
10736
# File 'lib/syntax_tree.rb', line 10734

def target
  @target
end

Instance Method Details

#child_nodesObject



10752
10753
10754
# File 'lib/syntax_tree.rb', line 10752

def child_nodes
  [target, bodystmt]
end

#format(q) ⇒ Object



10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
# File 'lib/syntax_tree.rb', line 10756

def format(q)
  q.group(0, "class << ", "end") do
    q.format(target)
    q.indent do
      q.breakable(force: true)
      q.format(bodystmt)
    end
    q.breakable(force: true)
  end
end

#pretty_print(q) ⇒ Object



10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
# File 'lib/syntax_tree.rb', line 10767

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

    q.breakable
    q.pp(target)

    q.breakable
    q.pp(bodystmt)

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

#to_json(*opts) ⇒ Object



10781
10782
10783
10784
10785
10786
10787
10788
10789
# File 'lib/syntax_tree.rb', line 10781

def to_json(*opts)
  {
    type: :sclass,
    target: target,
    bodystmt: bodystmt,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end