class Prism::ForNode - Documentation for Ruby 4.1
Description: class Prism::ForNode: Represents the use of the `for` keyword.
Keywords: ruby,class,Prism::ForNode

🔍

No recent searches

# class Prism::ForNode

Represents the use of the `for` keyword.

    for i in a end
    ^^^^^^^^^^^^^^

### Public Class Methods

new (source, node_id, location, flags, index, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_loc, end_keyword_loc)
Source

    # File lib/prism/node.rb, line 12171
    def initialize(source, node_id, location, flags, index, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_loc, end_keyword_loc)
      @source = source
      @node_id = node_id
      @location = location
      @flags = flags
      @index = index
      @collection = collection
      @statements = statements
      @for_keyword_loc = for_keyword_loc
      @in_keyword_loc = in_keyword_loc
      @do_keyword_loc = do_keyword_loc
      @end_keyword_loc = end_keyword_loc
    end

Initialize a new `ForNode` node.

### Public Instance Methods

collection → Node
Source

    # File lib/prism/node.rb, line 12300
    def collection
      @collection
    end

The collection to iterate over.

    for i in a end
             ^

index → LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode
Source

    # File lib/prism/node.rb, line 12287
    def index
      @index
    end

The index expression for `for` loops.

    for i in a end
        ^

statements → StatementsNode | nil
Source

    # File lib/prism/node.rb, line 12315
    def statements
      @statements
    end

Represents the body of statements to execute for each iteration of the loop.

    for i in a
      foo(i)
      ^^^^^^
    end

## Locations

### Public Instance Methods

do_keyword_loc → Location | nil
Source

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

The `Location` of the `do` keyword, if present.

    for i in a do end
               ^^

end_keyword_loc → Location
Source

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

The `Location` of the `end` keyword.

    for i in a end
               ^^^

for_keyword_loc → Location
Source

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

The `Location` of the `for` keyword.

    for i in a end
    ^^^

in_keyword_loc → Location
Source

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

The `Location` of the `in` keyword.

    for i in a end
          ^^

## 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 12267
    def self.type
      :for_node
    end

See `Node.type`.

### Public Instance Methods

accept (visitor)
Source

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

See `Node.accept`.

child_nodes ()
Source

    # File lib/prism/node.rb, line 12206
    def child_nodes
      [index, collection, statements]
    end

See `Node.child_nodes`.

Also aliased as: deconstruct
comment_targets ()
Source

    # File lib/prism/node.rb, line 12236
    def comment_targets
      [index, collection, *statements, for_keyword_loc, in_keyword_loc, *do_keyword_loc, end_keyword_loc] #: Array[Prism::node | Location]
    end

See `Node.comment_targets`.

compact_child_nodes ()
Source

    # File lib/prism/node.rb, line 12225
    def compact_child_nodes
      compact = [] #: Array[Prism::node]
      compact << index
      compact << collection
      if (statements = self.statements); compact << statements; end
      compact
    end

See `Node.compact_child_nodes`.

copy(**fields) → ForNode
Source

    # File lib/prism/node.rb, line 12246
    def copy(node_id: self.node_id, location: self.location, flags: self.flags, index: self.index, collection: self.collection, statements: self.statements, for_keyword_loc: self.for_keyword_loc, in_keyword_loc: self.in_keyword_loc, do_keyword_loc: self.do_keyword_loc, end_keyword_loc: self.end_keyword_loc)
      ForNode.new(source, node_id, location, flags, index, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_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 () { |index| ... }
Source

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

      yield index
      yield collection
      if (statements = self.statements); yield statements; end
    end

See `Node.each_child_node`.

type ()
Source

    # File lib/prism/node.rb, line 12260
    def type
      :for_node
    end

See `Node#type`.

## Repository

Methods related to `Relocation`.

### Public Instance Methods

save_do_keyword_loc (repository)
Source

    # File lib/prism/node.rb, line 12396
    def save_do_keyword_loc(repository)
      repository.enter(node_id, :do_keyword_loc) unless @do_keyword_loc.nil?
    end

Save the `do_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 12420
    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_for_keyword_loc (repository)
Source

    # File lib/prism/node.rb, line 12340
    def save_for_keyword_loc(repository)
      repository.enter(node_id, :for_keyword_loc)
    end

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

save_in_keyword_loc (repository)
Source

    # File lib/prism/node.rb, line 12365
    def save_in_keyword_loc(repository)
      repository.enter(node_id, :in_keyword_loc)
    end

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

## Slicing

### Public Instance Methods

do_keyword → String | nil
Source

    # File lib/prism/node.rb, line 12452
    def do_keyword
      do_keyword_loc&.slice
    end

Slice the location of `do_keyword_loc` from the source.

end_keyword → String
Source

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

Slice the location of `end_keyword_loc` from the source.

for_keyword → String
Source

    # File lib/prism/node.rb, line 12432
    def for_keyword
      for_keyword_loc.slice
    end

Slice the location of `for_keyword_loc` from the source.

in_keyword → String
Source

    # File lib/prism/node.rb, line 12442
    def in_keyword
      in_keyword_loc.slice
    end

Slice the location of `in_keyword_loc` from the source.