class Prism::SingletonClassNode - Documentation for Ruby 4.1
Description: class Prism::SingletonClassNode: Represents a singleton class declaration involving the `class` keyword.
Keywords: ruby,class,Prism::SingletonClassNode

🔍

No recent searches

# class Prism::SingletonClassNode

Represents a singleton class declaration involving the `class` keyword.

    class << self end
    ^^^^^^^^^^^^^^^^^

### Public Class Methods

new (source, node_id, location, flags, locals, class_keyword_loc, operator_loc, expression, body, end_keyword_loc)
Source

    # File lib/prism/node.rb, line 28046
    def initialize(source, node_id, location, flags, locals, class_keyword_loc, operator_loc, expression, body, end_keyword_loc)
      @source = source
      @node_id = node_id
      @location = location
      @flags = flags
      @locals = locals
      @class_keyword_loc = class_keyword_loc
      @operator_loc = operator_loc
      @expression = expression
      @body = body
      @end_keyword_loc = end_keyword_loc
    end

Initialize a new `SingletonClassNode` node.

### Public Instance Methods

body → StatementsNode | BeginNode | nil
Source

    # File lib/prism/node.rb, line 28220
    def body
      @body
    end

Returns the `body` attribute.

expression → Node
Source

    # File lib/prism/node.rb, line 28210
    def expression
      @expression
    end

Returns the `expression` attribute.

locals → Array[Symbol]
Source

    # File lib/prism/node.rb, line 28156
    def locals
      @locals
    end

Returns the `locals` attribute.

## Locations

### Public Instance Methods

class_keyword_loc → Location
Source

    # File lib/prism/node.rb, line 28167
    def class_keyword_loc
      location = @class_keyword_loc
      return location if location.is_a?(Location)
      @class_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
    end

Returns the `Location` represented by `class_keyword_loc`.

end_keyword_loc → Location
Source

    # File lib/prism/node.rb, line 28231
    def end_keyword_loc
      location = @end_keyword_loc
      return location if location.is_a?(Location)
      @end_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
    end

Returns the `Location` represented by `end_keyword_loc`.

operator_loc → Location
Source

    # File lib/prism/node.rb, line 28189
    def operator_loc
      location = @operator_loc
      return location if location.is_a?(Location)
      @operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
    end

Returns the `Location` represented by `operator_loc`.

## Node Interface

These methods are present on all subclasses of `Node`. Read the node interface docs for more information.

### Public Class Methods

type ()
Source

    # File lib/prism/node.rb, line 28139
    def self.type
      :singleton_class_node
    end

See `Node.type`.

### Public Instance Methods

accept (visitor)
Source

    # File lib/prism/node.rb, line 28073
    def accept(visitor)
      visitor.visit_singleton_class_node(self)
    end

See `Node.accept`.

child_nodes ()
Source

    # File lib/prism/node.rb, line 28080
    def child_nodes
      [expression, body]
    end

See `Node.child_nodes`.

Also aliased as: deconstruct
comment_targets ()
Source

    # File lib/prism/node.rb, line 28108
    def comment_targets
      [class_keyword_loc, operator_loc, expression, *body, end_keyword_loc] #: Array[Prism::node | Location]
    end

See `Node.comment_targets`.

compact_child_nodes ()
Source

    # File lib/prism/node.rb, line 28098
    def compact_child_nodes
      compact = [] #: Array[Prism::node]
      compact << expression
      if (body = self.body); compact << body; end
      compact
    end

See `Node.compact_child_nodes`.

copy(**fields) → SingletonClassNode
Source

    # File lib/prism/node.rb, line 28118
    def copy(node_id: self.node_id, location: self.location, flags: self.flags, locals: self.locals, class_keyword_loc: self.class_keyword_loc, operator_loc: self.operator_loc, expression: self.expression, body: self.body, end_keyword_loc: self.end_keyword_loc)
      SingletonClassNode.new(source, node_id, location, flags, locals, class_keyword_loc, operator_loc, expression, body, end_keyword_loc)
    end

Creates a copy of self with the given fields, using self as the template.

deconstruct ()
Alias for: child_nodes
each_child_node () { |expression| ... }
Source

    # File lib/prism/node.rb, line 28088
    def each_child_node(&blk)
      return to_enum(:each_child_node) unless block_given?

      yield expression
      if (body = self.body); yield body; end
    end

See `Node.each_child_node`.

type ()
Source

    # File lib/prism/node.rb, line 28132
    def type
      :singleton_class_node
    end

See `Node#type`.

## Repository

Methods related to `Relocation`.

### Public Instance Methods

save_class_keyword_loc (repository)
Source

    # File lib/prism/node.rb, line 28178
    def save_class_keyword_loc(repository)
      repository.enter(node_id, :class_keyword_loc)
    end

Save the `class_keyword_loc` location using the given saved source so that it can be retrieved later.

save_end_keyword_loc (repository)
Source

    # File lib/prism/node.rb, line 28242
    def save_end_keyword_loc(repository)
      repository.enter(node_id, :end_keyword_loc)
    end

Save the `end_keyword_loc` location using the given saved source so that it can be retrieved later.

save_operator_loc (repository)
Source

    # File lib/prism/node.rb, line 28200
    def save_operator_loc(repository)
      repository.enter(node_id, :operator_loc)
    end

Save the `operator_loc` location using the given saved source so that it can be retrieved later.

## Slicing

### Public Instance Methods

class_keyword → String
Source

    # File lib/prism/node.rb, line 28254
    def class_keyword
      class_keyword_loc.slice
    end

Slice the location of `class_keyword_loc` from the source.

end_keyword → String
Source

    # File lib/prism/node.rb, line 28274
    def end_keyword
      end_keyword_loc.slice
    end

Slice the location of `end_keyword_loc` from the source.

operator → String
Source

    # File lib/prism/node.rb, line 28264
    def operator
      operator_loc.slice
    end

Slice the location of `operator_loc` from the source.