class SyntaxTree::ClassDeclaration

Class represents defining a class using the class keyword.

class Container
end

Classes can have path names as their class name in case it’s being nested under a namespace, as in:

class Namespace::Container
end

Classes can also be defined as a top-level path, in the case that it’s already in a namespace but you want to define it at the top-level instead, as in:

module OtherNamespace
  class ::Namespace::Container
  end
end

All of these declarations can also have an optional superclass reference, as in:

class Child < Parent
end

That superclass can actually be any Ruby expression, it doesn’t necessarily need to be a constant, as in:

class Child < method
end

Attributes

bodystmt[R]
BodyStmt

the expressions to execute within the context of the class

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

constant[R]
ConstPathRef | ConstRef | TopConstRef

the name of the class being

defined

superclass[R]
nil | Node

the optional superclass declaration

Public Class Methods

new(constant:, superclass:, bodystmt:, location:) click to toggle source
# File lib/syntax_tree/node.rb, line 3329
def initialize(constant:, superclass:, bodystmt:, location:)
  @constant = constant
  @superclass = superclass
  @bodystmt = bodystmt
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 3392
def ===(other)
  other.is_a?(ClassDeclaration) && constant === other.constant &&
    superclass === other.superclass && bodystmt === other.bodystmt
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 3337
def accept(visitor)
  visitor.visit_class(self)
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 3341
def child_nodes
  [constant, superclass, bodystmt]
end
Also aliased as: deconstruct
copy(constant: nil, superclass: nil, bodystmt: nil, location: nil) click to toggle source
# File lib/syntax_tree/node.rb, line 3345
def copy(constant: nil, superclass: nil, bodystmt: nil, location: nil)
  node =
    ClassDeclaration.new(
      constant: constant || self.constant,
      superclass: superclass || self.superclass,
      bodystmt: bodystmt || self.bodystmt,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end
deconstruct()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 3360
def deconstruct_keys(_keys)
  {
    constant: constant,
    superclass: superclass,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 3370
def format(q)
  if bodystmt.empty?
    q.group do
      format_declaration(q)
      q.breakable_force
      q.text("end")
    end
  else
    q.group do
      format_declaration(q)

      q.indent do
        q.breakable_force
        q.format(bodystmt)
      end

      q.breakable_force
      q.text("end")
    end
  end
end