class Prism::DefNode - Documentation for Ruby 4.1
Description: class Prism::DefNode: Represents a method definition.
Keywords: ruby,class,Prism::DefNode

🔍

No recent searches

# class Prism::DefNode

Represents a method definition.

    def method
    end
    ^^^^^^^^^^

### Public Class Methods

new (source, node_id, location, flags, name, name_loc, receiver, parameters, body, locals, def_keyword_loc, operator_loc, lparen_loc, rparen_loc, equal_loc, end_keyword_loc)
Source

    # File lib/prism/node.rb, line 9881
    def initialize(source, node_id, location, flags, name, name_loc, receiver, parameters, body, locals, def_keyword_loc, operator_loc, lparen_loc, rparen_loc, equal_loc, end_keyword_loc)
      @source = source
      @node_id = node_id
      @location = location
      @flags = flags
      @name = name
      @name_loc = name_loc
      @receiver = receiver
      @parameters = parameters
      @body = body
      @locals = locals
      @def_keyword_loc = def_keyword_loc
      @operator_loc = operator_loc
      @lparen_loc = lparen_loc
      @rparen_loc = rparen_loc
      @equal_loc = equal_loc
      @end_keyword_loc = end_keyword_loc
    end

Initialize a new `DefNode` node.

### Public Instance Methods

body → StatementsNode | BeginNode | nil
Source

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

Returns the `body` attribute.

locals → Array[Symbol]
Source

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

Returns the `locals` attribute.

name → Symbol
Source

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

Returns the `name` attribute.

parameters → ParametersNode | nil
Source

    # File lib/prism/node.rb, line 10041
    def parameters
      @parameters
    end

Returns the `parameters` attribute.

receiver → Node | nil
Source

    # File lib/prism/node.rb, line 10031
    def receiver
      @receiver
    end

Returns the `receiver` attribute.

## Locations

### Public Instance Methods

def_keyword_loc → Location
Source

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

Returns the `Location` represented by `def_keyword_loc`.

end_keyword_loc → Location | nil
Source

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

Returns the `Location` represented by `end_keyword_loc`.

equal_loc → Location | nil
Source

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

Returns the `Location` represented by `equal_loc`.

lparen_loc → Location | nil
Source

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

Returns the `Location` represented by `lparen_loc`.

name_loc → Location
Source

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

Returns the `Location` represented by `name_loc`.

operator_loc → Location | nil
Source

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

Returns the `Location` represented by `operator_loc`.

rparen_loc → Location | nil
Source

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

Returns the `Location` represented by `rparen_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 9982
    def self.type
      :def_node
    end

See `Node.type`.

### Public Instance Methods

accept (visitor)
Source

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

See `Node.accept`.

child_nodes ()
Source

    # File lib/prism/node.rb, line 9921
    def child_nodes
      [receiver, parameters, body]
    end

See `Node.child_nodes`.

Also aliased as: deconstruct
comment_targets ()
Source

    # File lib/prism/node.rb, line 9951
    def comment_targets
      [name_loc, *receiver, *parameters, *body, def_keyword_loc, *operator_loc, *lparen_loc, *rparen_loc, *equal_loc, *end_keyword_loc] #: Array[Prism::node | Location]
    end

See `Node.comment_targets`.

compact_child_nodes ()
Source

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

See `Node.compact_child_nodes`.

copy(**fields) → DefNode
Source

    # File lib/prism/node.rb, line 9961
    def copy(node_id: self.node_id, location: self.location, flags: self.flags, name: self.name, name_loc: self.name_loc, receiver: self.receiver, parameters: self.parameters, body: self.body, locals: self.locals, def_keyword_loc: self.def_keyword_loc, operator_loc: self.operator_loc, lparen_loc: self.lparen_loc, rparen_loc: self.rparen_loc, equal_loc: self.equal_loc, end_keyword_loc: self.end_keyword_loc)
      DefNode.new(source, node_id, location, flags, name, name_loc, receiver, parameters, body, locals, def_keyword_loc, operator_loc, lparen_loc, rparen_loc, equal_loc, 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 () { |receiver| ... }
Source

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

      if (receiver = self.receiver); yield receiver; end
      if (parameters = self.parameters); yield parameters; end
      if (body = self.body); yield body; end
    end

See `Node.each_child_node`.

type ()
Source

    # File lib/prism/node.rb, line 9975
    def type
      :def_node
    end

See `Node#type`.

## Repository

Methods related to `Relocation`.

### Public Instance Methods

save_def_keyword_loc (repository)
Source

    # File lib/prism/node.rb, line 10083
    def save_def_keyword_loc(repository)
      repository.enter(node_id, :def_keyword_loc)
    end

Save the `def_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 10219
    def save_end_keyword_loc(repository)
      repository.enter(node_id, :end_keyword_loc) unless @end_keyword_loc.nil?
    end

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

save_equal_loc (repository)
Source

    # File lib/prism/node.rb, line 10192
    def save_equal_loc(repository)
      repository.enter(node_id, :equal_loc) unless @equal_loc.nil?
    end

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

save_lparen_loc (repository)
Source

    # File lib/prism/node.rb, line 10138
    def save_lparen_loc(repository)
      repository.enter(node_id, :lparen_loc) unless @lparen_loc.nil?
    end

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

save_name_loc (repository)
Source

    # File lib/prism/node.rb, line 10021
    def save_name_loc(repository)
      repository.enter(node_id, :name_loc)
    end

Save the `name_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 10111
    def save_operator_loc(repository)
      repository.enter(node_id, :operator_loc) unless @operator_loc.nil?
    end

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

save_rparen_loc (repository)
Source

    # File lib/prism/node.rb, line 10165
    def save_rparen_loc(repository)
      repository.enter(node_id, :rparen_loc) unless @rparen_loc.nil?
    end

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

## Slicing

### Public Instance Methods

def_keyword → String
Source

    # File lib/prism/node.rb, line 10230
    def def_keyword
      def_keyword_loc.slice
    end

Slice the location of `def_keyword_loc` from the source.

end_keyword → String | nil
Source

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

Slice the location of `end_keyword_loc` from the source.

equal → String | nil
Source

    # File lib/prism/node.rb, line 10270
    def equal
      equal_loc&.slice
    end

Slice the location of `equal_loc` from the source.

lparen → String | nil
Source

    # File lib/prism/node.rb, line 10250
    def lparen
      lparen_loc&.slice
    end

Slice the location of `lparen_loc` from the source.

operator → String | nil
Source

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

Slice the location of `operator_loc` from the source.

rparen → String | nil
Source

    # File lib/prism/node.rb, line 10260
    def rparen
      rparen_loc&.slice
    end

Slice the location of `rparen_loc` from the source.