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

🔍

No recent searches

# class Prism::ClassNode

Represents a class declaration involving the `class` keyword.

    class Foo end
    ^^^^^^^^^^^^^

### Public Class Methods

new (source, node_id, location, flags, locals, class_keyword_loc, constant_path, inheritance_operator_loc, superclass, body, end_keyword_loc, name)
Source

    # File lib/prism/node.rb, line 6270
    def initialize(source, node_id, location, flags, locals, class_keyword_loc, constant_path, inheritance_operator_loc, superclass, body, end_keyword_loc, name)
      @source = source
      @node_id = node_id
      @location = location
      @flags = flags
      @locals = locals
      @class_keyword_loc = class_keyword_loc
      @constant_path = constant_path
      @inheritance_operator_loc = inheritance_operator_loc
      @superclass = superclass
      @body = body
      @end_keyword_loc = end_keyword_loc
      @name = name
    end

Initialize a new `ClassNode` node.

### Public Instance Methods

body → StatementsNode | BeginNode | nil
Source

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

Represents the body of the class.

    class Foo; bar; end
               ^^^

constant_path → ConstantReadNode | ConstantPathNode
Source

    # File lib/prism/node.rb, line 6419
    def constant_path
      @constant_path
    end

Returns the `constant_path` attribute.

locals → Array[Symbol]
Source

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

Returns the `locals` attribute.

name → Symbol
Source

    # File lib/prism/node.rb, line 6512
    def name
      @name
    end

The name of the class.

    class Foo end # name `:Foo`

superclass → Node | nil
Source

    # File lib/prism/node.rb, line 6462
    def superclass
      @superclass
    end

Represents the superclass of the class.

    class Foo < Bar
                ^^^

## Locations

### Public Instance Methods

class_keyword_loc → Location
Source

    # File lib/prism/node.rb, line 6398
    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

Represents the `Location` of the `class` keyword.

    class Foo end
    ^^^^^

end_keyword_loc → Location
Source

    # File lib/prism/node.rb, line 6489
    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

Represents the `Location` of the `end` keyword.

    class Foo end
              ^^^

inheritance_operator_loc → Location | nil
Source

    # File lib/prism/node.rb, line 6433
    def inheritance_operator_loc
      location = @inheritance_operator_loc
      case location
      when nil
        nil
      when Location
        location
      else
        @inheritance_operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
      end
    end

Represents the `Location` of the `<` operator.

    class Foo < Bar
              ^

## 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 6367
    def self.type
      :class_node
    end

See `Node.type`.

### Public Instance Methods

accept (visitor)
Source

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

See `Node.accept`.

child_nodes ()
Source

    # File lib/prism/node.rb, line 6306
    def child_nodes
      [constant_path, superclass, body]
    end

See `Node.child_nodes`.

Also aliased as: deconstruct
comment_targets ()
Source

    # File lib/prism/node.rb, line 6336
    def comment_targets
      [class_keyword_loc, constant_path, *inheritance_operator_loc, *superclass, *body, end_keyword_loc] #: Array[Prism::node | Location]
    end

See `Node.comment_targets`.

compact_child_nodes ()
Source

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

See `Node.compact_child_nodes`.

copy(**fields) → ClassNode
Source

    # File lib/prism/node.rb, line 6346
    def copy(node_id: self.node_id, location: self.location, flags: self.flags, locals: self.locals, class_keyword_loc: self.class_keyword_loc, constant_path: self.constant_path, inheritance_operator_loc: self.inheritance_operator_loc, superclass: self.superclass, body: self.body, end_keyword_loc: self.end_keyword_loc, name: self.name)
      ClassNode.new(source, node_id, location, flags, locals, class_keyword_loc, constant_path, inheritance_operator_loc, superclass, body, end_keyword_loc, name)
    end

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

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

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

      yield constant_path
      if (superclass = self.superclass); yield superclass; end
      if (body = self.body); yield body; end
    end

See `Node.each_child_node`.

type ()
Source

    # File lib/prism/node.rb, line 6360
    def type
      :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 6409
    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 6500
    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_inheritance_operator_loc (repository)
Source

    # File lib/prism/node.rb, line 6450
    def save_inheritance_operator_loc(repository)
      repository.enter(node_id, :inheritance_operator_loc) unless @inheritance_operator_loc.nil?
    end

Save the `inheritance_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 6524
    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 6544
    def end_keyword
      end_keyword_loc.slice
    end

Slice the location of `end_keyword_loc` from the source.

inheritance_operator → String | nil
Source

    # File lib/prism/node.rb, line 6534
    def inheritance_operator
      inheritance_operator_loc&.slice
    end

Slice the location of `inheritance_operator_loc` from the source.