class Prism::ElseNode - Documentation for Ruby 4.1
Description: class Prism::ElseNode: Represents an `else` clause in a `case`, `if`, or `unless` statement.
Keywords: ruby,class,Prism::ElseNode

🔍

No recent searches

# class Prism::ElseNode

Represents an `else` clause in a `case`, `if`, or `unless` statement.

    if a then b else c end
                ^^^^^^^^^^

### Public Class Methods

new (source, node_id, location, flags, else_keyword_loc, statements, end_keyword_loc)
Source

    # File lib/prism/node.rb, line 10558
    def initialize(source, node_id, location, flags, else_keyword_loc, statements, end_keyword_loc)
      @source = source
      @node_id = node_id
      @location = location
      @flags = flags
      @else_keyword_loc = else_keyword_loc
      @statements = statements
      @end_keyword_loc = end_keyword_loc
    end

Initialize a new `ElseNode` node.

### Public Instance Methods

statements → StatementsNode | nil
Source

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

Returns the `statements` attribute.

## Locations

### Public Instance Methods

else_keyword_loc → Location
Source

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

Returns the `Location` represented by `else_keyword_loc`.

end_keyword_loc → Location | nil
Source

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

## 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 10646
    def self.type
      :else_node
    end

See `Node.type`.

### Public Instance Methods

accept (visitor)
Source

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

See `Node.accept`.

child_nodes ()
Source

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

See `Node.child_nodes`.

Also aliased as: deconstruct
comment_targets ()
Source

    # File lib/prism/node.rb, line 10615
    def comment_targets
      [else_keyword_loc, *statements, *end_keyword_loc] #: Array[Prism::node | Location]
    end

See `Node.comment_targets`.

compact_child_nodes ()
Source

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

See `Node.compact_child_nodes`.

copy(**fields) → ElseNode
Source

    # File lib/prism/node.rb, line 10625
    def copy(node_id: self.node_id, location: self.location, flags: self.flags, else_keyword_loc: self.else_keyword_loc, statements: self.statements, end_keyword_loc: self.end_keyword_loc)
      ElseNode.new(source, node_id, location, flags, else_keyword_loc, statements, 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 10597
    def each_child_node(&blk)
      return to_enum(:each_child_node) unless block_given?

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

See `Node.each_child_node`.

type ()
Source

    # File lib/prism/node.rb, line 10639
    def type
      :else_node
    end

See `Node#type`.

## Repository

Methods related to `Relocation`.

### Public Instance Methods

save_else_keyword_loc (repository)
Source

    # File lib/prism/node.rb, line 10675
    def save_else_keyword_loc(repository)
      repository.enter(node_id, :else_keyword_loc)
    end

Save the `else_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 10713
    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

else_keyword → String
Source

    # File lib/prism/node.rb, line 10724
    def else_keyword
      else_keyword_loc.slice
    end

Slice the location of `else_keyword_loc` from the source.

end_keyword → String | nil
Source

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

Slice the location of `end_keyword_loc` from the source.