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



10877
10878
10879
10880
10881
10882
# File 'lib/syntax_tree.rb', line 10877

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



10869
10870
10871
# File 'lib/syntax_tree.rb', line 10869

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10875
10876
10877
# File 'lib/syntax_tree.rb', line 10875

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



10872
10873
10874
# File 'lib/syntax_tree.rb', line 10872

def location
  @location
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



10866
10867
10868
# File 'lib/syntax_tree.rb', line 10866

def target
  @target
end

Instance Method Details

#child_nodesObject



10884
10885
10886
# File 'lib/syntax_tree.rb', line 10884

def child_nodes
  [target, bodystmt]
end

#format(q) ⇒ Object



10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
# File 'lib/syntax_tree.rb', line 10888

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



10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
# File 'lib/syntax_tree.rb', line 10899

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



10913
10914
10915
10916
10917
10918
10919
10920
10921
# File 'lib/syntax_tree.rb', line 10913

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