Class: SyntaxTree::SClass

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



10215
10216
10217
10218
10219
10220
# File 'lib/syntax_tree.rb', line 10215

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



10207
10208
10209
# File 'lib/syntax_tree.rb', line 10207

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10213
10214
10215
# File 'lib/syntax_tree.rb', line 10213

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



10210
10211
10212
# File 'lib/syntax_tree.rb', line 10210

def location
  @location
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



10204
10205
10206
# File 'lib/syntax_tree.rb', line 10204

def target
  @target
end

Instance Method Details

#child_nodesObject



10222
10223
10224
# File 'lib/syntax_tree.rb', line 10222

def child_nodes
  [target, bodystmt]
end

#format(q) ⇒ Object



10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
# File 'lib/syntax_tree.rb', line 10226

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



10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
# File 'lib/syntax_tree.rb', line 10237

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



10251
10252
10253
10254
10255
10256
10257
10258
10259
# File 'lib/syntax_tree.rb', line 10251

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