class Prism::WhileNode - Documentation for Ruby 4.1
Description: class Prism::WhileNode: Represents the use of the `while` keyword, either in the block form or the modifier form.
Keywords: ruby,class,Prism::WhileNode

🔍

No recent searches

# class Prism::WhileNode

Represents the use of the `while` keyword, either in the block form or the modifier form.

    bar while foo
    ^^^^^^^^^^^^^

    while foo do bar end
    ^^^^^^^^^^^^^^^^^^^^

### Public Class Methods

new (source, node_id, location, flags, keyword_loc, do_keyword_loc, closing_loc, predicate, statements)
Source

    # File lib/prism/node.rb, line 30863
    def initialize(source, node_id, location, flags, keyword_loc, do_keyword_loc, closing_loc, predicate, statements)
      @source = source
      @node_id = node_id
      @location = location
      @flags = flags
      @keyword_loc = keyword_loc
      @do_keyword_loc = do_keyword_loc
      @closing_loc = closing_loc
      @predicate = predicate
      @statements = statements
    end

Initialize a new `WhileNode` node.

### Public Instance Methods

predicate → Node
Source

    # File lib/prism/node.rb, line 31056
    def predicate
      @predicate
    end

Returns the `predicate` attribute.

statements → StatementsNode | nil
Source

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

Returns the `statements` attribute.

## Flags

### Public Instance Methods

begin_modifier? ()
Source

    # File lib/prism/node.rb, line 30970
    def begin_modifier?
      flags.anybits?(LoopFlags::BEGIN_MODIFIER)
    end

a loop after a begin statement, so the body is executed first before the condition

## Locations

### Public Instance Methods

closing_loc → Location | nil
Source

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

Returns the `Location` represented by `closing_loc`.

do_keyword_loc → Location | nil
Source

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

Returns the `Location` represented by `do_keyword_loc`.

keyword_loc → Location
Source

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

Returns the `Location` represented by `keyword_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 30955
    def self.type
      :while_node
    end

See `Node.type`.

### Public Instance Methods

accept (visitor)
Source

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

See `Node.accept`.

child_nodes ()
Source

    # File lib/prism/node.rb, line 30896
    def child_nodes
      [predicate, statements]
    end

See `Node.child_nodes`.

Also aliased as: deconstruct
comment_targets ()
Source

    # File lib/prism/node.rb, line 30924
    def comment_targets
      [keyword_loc, *do_keyword_loc, *closing_loc, predicate, *statements] #: Array[Prism::node | Location]
    end

See `Node.comment_targets`.

compact_child_nodes ()
Source

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

See `Node.compact_child_nodes`.

copy(**fields) → WhileNode
Source

    # File lib/prism/node.rb, line 30934
    def copy(node_id: self.node_id, location: self.location, flags: self.flags, keyword_loc: self.keyword_loc, do_keyword_loc: self.do_keyword_loc, closing_loc: self.closing_loc, predicate: self.predicate, statements: self.statements)
      WhileNode.new(source, node_id, location, flags, keyword_loc, do_keyword_loc, closing_loc, predicate, statements)
    end

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

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

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

      yield predicate
      if (statements = self.statements); yield statements; end
    end

See `Node.each_child_node`.

type ()
Source

    # File lib/prism/node.rb, line 30948
    def type
      :while_node
    end

See `Node#type`.

## Repository

Methods related to `Relocation`.

### Public Instance Methods

save_closing_loc (repository)
Source

    # File lib/prism/node.rb, line 31047
    def save_closing_loc(repository)
      repository.enter(node_id, :closing_loc) unless @closing_loc.nil?
    end

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

save_do_keyword_loc (repository)
Source

    # File lib/prism/node.rb, line 31020
    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_keyword_loc (repository)
Source

    # File lib/prism/node.rb, line 30992
    def save_keyword_loc(repository)
      repository.enter(node_id, :keyword_loc)
    end

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

## Slicing

### Public Instance Methods

closing → String | nil
Source

    # File lib/prism/node.rb, line 31098
    def closing
      closing_loc&.slice
    end

Slice the location of `closing_loc` from the source.

do_keyword → String | nil
Source

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

Slice the location of `do_keyword_loc` from the source.

keyword → String
Source

    # File lib/prism/node.rb, line 31078
    def keyword
      keyword_loc.slice
    end

Slice the location of `keyword_loc` from the source.