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.



8240
8241
8242
8243
8244
8245
# File 'lib/syntax_tree/node.rb', line 8240

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



8235
8236
8237
# File 'lib/syntax_tree/node.rb', line 8235

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8238
8239
8240
# File 'lib/syntax_tree/node.rb', line 8238

def comments
  @comments
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



8232
8233
8234
# File 'lib/syntax_tree/node.rb', line 8232

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



8247
8248
8249
# File 'lib/syntax_tree/node.rb', line 8247

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

#child_nodesObject Also known as: deconstruct



8251
8252
8253
# File 'lib/syntax_tree/node.rb', line 8251

def child_nodes
  [target, bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



8257
8258
8259
8260
8261
8262
8263
8264
# File 'lib/syntax_tree/node.rb', line 8257

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

#format(q) ⇒ Object



8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
# File 'lib/syntax_tree/node.rb', line 8266

def format(q)
  q.text("class << ")
  q.group do
    q.format(target)
    q.indent do
      q.breakable_force
      q.format(bodystmt)
    end
    q.breakable_force
  end
  q.text("end")
end