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.



7222
7223
7224
7225
7226
7227
# File 'lib/syntax_tree/node.rb', line 7222

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



7217
7218
7219
# File 'lib/syntax_tree/node.rb', line 7217

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7220
7221
7222
# File 'lib/syntax_tree/node.rb', line 7220

def comments
  @comments
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



7214
7215
7216
# File 'lib/syntax_tree/node.rb', line 7214

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



7229
7230
7231
# File 'lib/syntax_tree/node.rb', line 7229

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

#child_nodesObject Also known as: deconstruct



7233
7234
7235
# File 'lib/syntax_tree/node.rb', line 7233

def child_nodes
  [target, bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



7239
7240
7241
7242
7243
7244
7245
7246
# File 'lib/syntax_tree/node.rb', line 7239

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

#format(q) ⇒ Object



7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
# File 'lib/syntax_tree/node.rb', line 7248

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