Class: SyntaxTree::SClass

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

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of SClass.



7764
7765
7766
7767
7768
7769
# File 'lib/syntax_tree/node.rb', line 7764

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



7759
7760
7761
# File 'lib/syntax_tree/node.rb', line 7759

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7762
7763
7764
# File 'lib/syntax_tree/node.rb', line 7762

def comments
  @comments
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



7756
7757
7758
# File 'lib/syntax_tree/node.rb', line 7756

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



7771
7772
7773
# File 'lib/syntax_tree/node.rb', line 7771

def accept(visitor)
  visitor.visit_sclass(self)
end

#child_nodesObject Also known as: deconstruct



7775
7776
7777
# File 'lib/syntax_tree/node.rb', line 7775

def child_nodes
  [target, bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



7781
7782
7783
7784
7785
7786
7787
7788
# File 'lib/syntax_tree/node.rb', line 7781

def deconstruct_keys(keys)
  {
    target: target,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
# File 'lib/syntax_tree/node.rb', line 7790

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