class Prism::IfNode - Documentation for Ruby 4.1
Description: class Prism::IfNode: Represents the use of the `if` keyword, either in the block form or the modifier form, or a ternary expression.
Keywords: ruby,class,Prism::IfNode

🔍

No recent searches

# class Prism::IfNode

Represents the use of the `if` keyword, either in the block form or the modifier form, or a ternary expression.

    bar if foo
    ^^^^^^^^^^

    if foo then bar end
    ^^^^^^^^^^^^^^^^^^^

    foo ? bar : baz
    ^^^^^^^^^^^^^^^

### Public Class Methods

new (source, node_id, location, flags, if_keyword_loc, predicate, then_keyword_loc, statements, subsequent, end_keyword_loc)
Source

    # File lib/prism/node.rb, line 14457
    def initialize(source, node_id, location, flags, if_keyword_loc, predicate, then_keyword_loc, statements, subsequent, end_keyword_loc)
      @source = source
      @node_id = node_id
      @location = location
      @flags = flags
      @if_keyword_loc = if_keyword_loc
      @predicate = predicate
      @then_keyword_loc = then_keyword_loc
      @statements = statements
      @subsequent = subsequent
      @end_keyword_loc = end_keyword_loc
    end

Initialize a new `IfNode` node.

### Public Instance Methods

predicate → Node
Source

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

The node for the condition the `IfNode` is testing.

    if foo
       ^^^
      bar
    end

    bar if foo
           ^^^

    foo ? bar : baz
    ^^^

statements → StatementsNode | nil
Source

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

Represents the body of statements that will be executed when the predicate is evaluated as truthy. Will be `nil` when no body is provided.

    if foo
      bar
      ^^^
      baz
      ^^^
    end

subsequent → ElseNode | IfNode | nil
Source

    # File lib/prism/node.rb, line 14684
    def subsequent
      @subsequent
    end

Represents an `ElseNode` or an `IfNode` when there is an `else` or an `elsif` in the `if` statement.

    if foo
      bar
    elsif baz
    ^^^^^^^^^
      qux
      ^^^
    end
    ^^^

    if foo then bar else baz end
                    ^^^^^^^^^^^^

## Locations

### Public Instance Methods

end_keyword_loc → Location | nil
Source

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

The `Location` of the `end` keyword if present, `nil` otherwise.

    if foo
      bar
    end
    ^^^

if_keyword_loc → Location | nil
Source

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

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

    bar if foo
        ^^

The `if_keyword_loc` field will be `nil` when the `IfNode` represents a ternary expression.

then_keyword_loc → Location | nil
Source

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

The `Location` of the `then` keyword (if present) or the `?` in a ternary expression, `nil` otherwise.

    if foo then bar end
           ^^^^

    a ? b : c
      ^

## 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 14552
    def self.type
      :if_node
    end

See `Node.type`.

### Public Instance Methods

accept (visitor)
Source

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

See `Node.accept`.

child_nodes ()
Source

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

See `Node.child_nodes`.

Also aliased as: deconstruct
comment_targets ()
Source

    # File lib/prism/node.rb, line 14521
    def comment_targets
      [*if_keyword_loc, predicate, *then_keyword_loc, *statements, *subsequent, *end_keyword_loc] #: Array[Prism::node | Location]
    end

See `Node.comment_targets`.

compact_child_nodes ()
Source

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

See `Node.compact_child_nodes`.

copy(**fields) → IfNode
Source

    # File lib/prism/node.rb, line 14531
    def copy(node_id: self.node_id, location: self.location, flags: self.flags, if_keyword_loc: self.if_keyword_loc, predicate: self.predicate, then_keyword_loc: self.then_keyword_loc, statements: self.statements, subsequent: self.subsequent, end_keyword_loc: self.end_keyword_loc)
      IfNode.new(source, node_id, location, flags, if_keyword_loc, predicate, then_keyword_loc, statements, subsequent, 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 () { |predicate| ... }
Source

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

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

See `Node.each_child_node`.

type ()
Source

    # File lib/prism/node.rb, line 14545
    def type
      :if_node
    end

See `Node#type`.

## Repository

Methods related to `Relocation`.

### Public Instance Methods

save_end_keyword_loc (repository)
Source

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

    # File lib/prism/node.rb, line 14592
    def save_if_keyword_loc(repository)
      repository.enter(node_id, :if_keyword_loc) unless @if_keyword_loc.nil?
    end

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

save_then_keyword_loc (repository)
Source

    # File lib/prism/node.rb, line 14646
    def save_then_keyword_loc(repository)
      repository.enter(node_id, :then_keyword_loc) unless @then_keyword_loc.nil?
    end

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

## Slicing

### Public Instance Methods

end_keyword → String | nil
Source

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

Slice the location of `end_keyword_loc` from the source.

if_keyword → String | nil
Source

    # File lib/prism/node.rb, line 14728
    def if_keyword
      if_keyword_loc&.slice
    end

Slice the location of `if_keyword_loc` from the source.

then_keyword → String | nil
Source

    # File lib/prism/node.rb, line 14738
    def then_keyword
      then_keyword_loc&.slice
    end

Slice the location of `then_keyword_loc` from the source.