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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SClass.



9285
9286
9287
9288
9289
9290
# File 'lib/syntax_tree/node.rb', line 9285

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



9277
9278
9279
# File 'lib/syntax_tree/node.rb', line 9277

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9283
9284
9285
# File 'lib/syntax_tree/node.rb', line 9283

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



9280
9281
9282
# File 'lib/syntax_tree/node.rb', line 9280

def location
  @location
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



9274
9275
9276
# File 'lib/syntax_tree/node.rb', line 9274

def target
  @target
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



9292
9293
9294
# File 'lib/syntax_tree/node.rb', line 9292

def child_nodes
  [target, bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



9298
9299
9300
9301
9302
9303
9304
9305
# File 'lib/syntax_tree/node.rb', line 9298

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

#format(q) ⇒ Object



9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
# File 'lib/syntax_tree/node.rb', line 9307

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

#pretty_print(q) ⇒ Object



9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
# File 'lib/syntax_tree/node.rb', line 9318

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("sclass")

    q.breakable
    q.pp(target)

    q.breakable
    q.pp(bodystmt)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



9332
9333
9334
9335
9336
9337
9338
9339
9340
# File 'lib/syntax_tree/node.rb', line 9332

def to_json(*opts)
  {
    type: :sclass,
    target: target,
    bodystmt: bodystmt,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end