class Prism::BeginNode - Documentation for Ruby 4.1
Description: class Prism::BeginNode: Represents a begin statement.
Keywords: ruby,class,Prism::BeginNode

🔍

No recent searches

# class Prism::BeginNode

Represents a begin statement.

    begin
      foo
    end
    ^^^^^

### Public Class Methods

new (source, node_id, location, flags, begin_keyword_loc, statements, rescue_clause, else_clause, ensure_clause, end_keyword_loc)
Source

    # File lib/prism/node.rb, line 2393
    def initialize(source, node_id, location, flags, begin_keyword_loc, statements, rescue_clause, else_clause, ensure_clause, end_keyword_loc)
      @source = source
      @node_id = node_id
      @location = location
      @flags = flags
      @begin_keyword_loc = begin_keyword_loc
      @statements = statements
      @rescue_clause = rescue_clause
      @else_clause = else_clause
      @ensure_clause = ensure_clause
      @end_keyword_loc = end_keyword_loc
    end

Initialize a new `BeginNode` node.

### Public Instance Methods

else_clause → ElseNode | nil
Source

    # File lib/prism/node.rb, line 2566
    def else_clause
      @else_clause
    end

Represents the else clause within the begin block.

    begin x; rescue y; else z; end
                       ^^^^^^^^^^^

ensure_clause → EnsureNode | nil
Source

    # File lib/prism/node.rb, line 2579
    def ensure_clause
      @ensure_clause
    end

Represents the ensure clause within the begin block.

    begin x; ensure y; end
             ^^^^^^^^

rescue_clause → RescueNode | nil
Source

    # File lib/prism/node.rb, line 2553
    def rescue_clause
      @rescue_clause
    end

Represents the rescue clause within the begin block.

    begin x; rescue y; end
             ^^^^^^^^

statements → StatementsNode | nil
Source

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

Represents the statements within the begin block.

    begin x end
          ^

## Locations

### Public Instance Methods

begin_keyword_loc → Location | nil
Source

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

Represents the `Location` of the `begin` keyword.

    begin x end
    ^^^^^

end_keyword_loc → Location | nil
Source

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

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

    begin x 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 2490
    def self.type
      :begin_node
    end

See `Node.type`.

### Public Instance Methods

accept (visitor)
Source

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

See `Node.accept`.

child_nodes ()
Source

    # File lib/prism/node.rb, line 2427
    def child_nodes
      [statements, rescue_clause, else_clause, ensure_clause]
    end

See `Node.child_nodes`.

Also aliased as: deconstruct
comment_targets ()
Source

    # File lib/prism/node.rb, line 2459
    def comment_targets
      [*begin_keyword_loc, *statements, *rescue_clause, *else_clause, *ensure_clause, *end_keyword_loc] #: Array[Prism::node | Location]
    end

See `Node.comment_targets`.

compact_child_nodes ()
Source

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

See `Node.compact_child_nodes`.

copy(**fields) → BeginNode
Source

    # File lib/prism/node.rb, line 2469
    def copy(node_id: self.node_id, location: self.location, flags: self.flags, begin_keyword_loc: self.begin_keyword_loc, statements: self.statements, rescue_clause: self.rescue_clause, else_clause: self.else_clause, ensure_clause: self.ensure_clause, end_keyword_loc: self.end_keyword_loc)
      BeginNode.new(source, node_id, location, flags, begin_keyword_loc, statements, rescue_clause, else_clause, ensure_clause, 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 () { |statements| ... }
Source

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

      if (statements = self.statements); yield statements; end
      if (rescue_clause = self.rescue_clause); yield rescue_clause; end
      if (else_clause = self.else_clause); yield else_clause; end
      if (ensure_clause = self.ensure_clause); yield ensure_clause; end
    end

See `Node.each_child_node`.

type ()
Source

    # File lib/prism/node.rb, line 2483
    def type
      :begin_node
    end

See `Node#type`.

## Repository

Methods related to `Relocation`.

### Public Instance Methods

save_begin_keyword_loc (repository)
Source

    # File lib/prism/node.rb, line 2528
    def save_begin_keyword_loc(repository)
      repository.enter(node_id, :begin_keyword_loc) unless @begin_keyword_loc.nil?
    end

Save the `begin_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 2610
    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.

## Slicing

### Public Instance Methods

begin_keyword → String | nil
Source

    # File lib/prism/node.rb, line 2621
    def begin_keyword
      begin_keyword_loc&.slice
    end

Slice the location of `begin_keyword_loc` from the source.

end_keyword → String | nil
Source

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

Slice the location of `end_keyword_loc` from the source.