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.



7864
7865
7866
7867
7868
7869
# File 'lib/syntax_tree/node.rb', line 7864

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



7859
7860
7861
# File 'lib/syntax_tree/node.rb', line 7859

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7862
7863
7864
# File 'lib/syntax_tree/node.rb', line 7862

def comments
  @comments
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



7856
7857
7858
# File 'lib/syntax_tree/node.rb', line 7856

def target
  @target
end

Instance Method Details

#accept(visitor) ⇒ Object



7871
7872
7873
# File 'lib/syntax_tree/node.rb', line 7871

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

#child_nodesObject Also known as: deconstruct



7875
7876
7877
# File 'lib/syntax_tree/node.rb', line 7875

def child_nodes
  [target, bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



7881
7882
7883
7884
7885
7886
7887
7888
# File 'lib/syntax_tree/node.rb', line 7881

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

#format(q) ⇒ Object



7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
# File 'lib/syntax_tree/node.rb', line 7890

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