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.



7986
7987
7988
7989
7990
7991
# File 'lib/syntax_tree/node.rb', line 7986

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



7981
7982
7983
# File 'lib/syntax_tree/node.rb', line 7981

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7984
7985
7986
# File 'lib/syntax_tree/node.rb', line 7984

def comments
  @comments
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



7978
7979
7980
# File 'lib/syntax_tree/node.rb', line 7978

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



7993
7994
7995
# File 'lib/syntax_tree/node.rb', line 7993

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

#child_nodesObject Also known as: deconstruct



7997
7998
7999
# File 'lib/syntax_tree/node.rb', line 7997

def child_nodes
  [target, bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



8003
8004
8005
8006
8007
8008
8009
8010
# File 'lib/syntax_tree/node.rb', line 8003

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

#format(q) ⇒ Object



8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
# File 'lib/syntax_tree/node.rb', line 8012

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