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

Constructor Details

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

Returns a new instance of SClass.



8937
8938
8939
8940
8941
8942
# File 'lib/syntax_tree/node.rb', line 8937

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



8932
8933
8934
# File 'lib/syntax_tree/node.rb', line 8932

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8935
8936
8937
# File 'lib/syntax_tree/node.rb', line 8935

def comments
  @comments
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



8929
8930
8931
# File 'lib/syntax_tree/node.rb', line 8929

def target
  @target
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



8944
8945
8946
# File 'lib/syntax_tree/node.rb', line 8944

def child_nodes
  [target, bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



8950
8951
8952
8953
8954
8955
8956
8957
# File 'lib/syntax_tree/node.rb', line 8950

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

#format(q) ⇒ Object



8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
# File 'lib/syntax_tree/node.rb', line 8959

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



8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
# File 'lib/syntax_tree/node.rb', line 8970

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



8984
8985
8986
8987
8988
8989
8990
8991
8992
# File 'lib/syntax_tree/node.rb', line 8984

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