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

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of SClass.



8028
8029
8030
8031
8032
8033
# File 'lib/syntax_tree/node.rb', line 8028

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



8023
8024
8025
# File 'lib/syntax_tree/node.rb', line 8023

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8026
8027
8028
# File 'lib/syntax_tree/node.rb', line 8026

def comments
  @comments
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



8020
8021
8022
# File 'lib/syntax_tree/node.rb', line 8020

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



8035
8036
8037
# File 'lib/syntax_tree/node.rb', line 8035

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

#child_nodesObject Also known as: deconstruct



8039
8040
8041
# File 'lib/syntax_tree/node.rb', line 8039

def child_nodes
  [target, bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



8045
8046
8047
8048
8049
8050
8051
8052
# File 'lib/syntax_tree/node.rb', line 8045

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

#format(q) ⇒ Object



8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
# File 'lib/syntax_tree/node.rb', line 8054

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