class Prism::WhenNode - Documentation for Ruby 4.1
Description: class Prism::WhenNode: Represents the use of the `when` keyword within a case statement.
Keywords: ruby,class,Prism::WhenNode

🔍

No recent searches

# class Prism::WhenNode

Represents the use of the `when` keyword within a case statement.

    case true
    when true
    ^^^^^^^^^
    end

### Public Class Methods

new (source, node_id, location, flags, keyword_loc, conditions, then_keyword_loc, statements)
Source

    # File lib/prism/node.rb, line 30640
    def initialize(source, node_id, location, flags, keyword_loc, conditions, then_keyword_loc, statements)
      @source = source
      @node_id = node_id
      @location = location
      @flags = flags
      @keyword_loc = keyword_loc
      @conditions = conditions
      @then_keyword_loc = then_keyword_loc
      @statements = statements
    end

Initialize a new `WhenNode` node.

### Public Instance Methods

conditions → Array[Node]
Source

    # File lib/prism/node.rb, line 30770
    def conditions
      @conditions
    end

Returns the `conditions` attribute.

statements → StatementsNode | nil
Source

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

Returns the `statements` attribute.

## Locations

### Public Instance Methods

keyword_loc → Location
Source

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

then_keyword_loc → Location | nil
Source

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

Returns the `Location` represented by `then_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 30731
    def self.type
      :when_node
    end

See `Node.type`.

### Public Instance Methods

accept (visitor)
Source

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

See `Node.accept`.

child_nodes ()
Source

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

See `Node.child_nodes`.

Also aliased as: deconstruct
comment_targets ()
Source

    # File lib/prism/node.rb, line 30700
    def comment_targets
      [keyword_loc, *conditions, *then_keyword_loc, *statements] #: Array[Prism::node | Location]
    end

See `Node.comment_targets`.

compact_child_nodes ()
Source

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

See `Node.compact_child_nodes`.

copy(**fields) → WhenNode
Source

    # File lib/prism/node.rb, line 30710
    def copy(node_id: self.node_id, location: self.location, flags: self.flags, keyword_loc: self.keyword_loc, conditions: self.conditions, then_keyword_loc: self.then_keyword_loc, statements: self.statements)
      WhenNode.new(source, node_id, location, flags, keyword_loc, conditions, then_keyword_loc, statements)
    end

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

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

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

      conditions.each { |node| yield node }
      if (statements = self.statements); yield statements; end
    end

See `Node.each_child_node`.

type ()
Source

    # File lib/prism/node.rb, line 30724
    def type
      :when_node
    end

See `Node#type`.

## Repository

Methods related to `Relocation`.

### Public Instance Methods

save_keyword_loc (repository)
Source

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

save_then_keyword_loc (repository)
Source

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

keyword → String
Source

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

Slice the location of `keyword_loc` from the source.

then_keyword → String | nil
Source

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

Slice the location of `then_keyword_loc` from the source.