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.



10669
10670
10671
10672
10673
10674
# File 'lib/syntax_tree.rb', line 10669

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



10661
10662
10663
# File 'lib/syntax_tree.rb', line 10661

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10667
10668
10669
# File 'lib/syntax_tree.rb', line 10667

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



10664
10665
10666
# File 'lib/syntax_tree.rb', line 10664

def location
  @location
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



10658
10659
10660
# File 'lib/syntax_tree.rb', line 10658

def target
  @target
end

Instance Method Details

#child_nodesObject



10676
10677
10678
# File 'lib/syntax_tree.rb', line 10676

def child_nodes
  [target, bodystmt]
end

#format(q) ⇒ Object



10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
# File 'lib/syntax_tree.rb', line 10680

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



10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
# File 'lib/syntax_tree.rb', line 10691

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



10705
10706
10707
10708
10709
10710
10711
10712
10713
# File 'lib/syntax_tree.rb', line 10705

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