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

# class Prism::WhenNode

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

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

### Attributes

conditions [R]

attr_reader conditions: Array

statements [R]

attr_reader statements: `StatementsNode`?

### Public Class Methods

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

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

type ()
Source

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

Return a symbol representation of this node type. See `Node::type`.

### Public Instance Methods

=== (other)
Source

    # File lib/prism/node.rb, line 18166
    def ===(other)
      other.is_a?(WhenNode) &&
        (keyword_loc.nil? == other.keyword_loc.nil?) &&
        (conditions.length == other.conditions.length) &&
        conditions.zip(other.conditions).all? { |left, right| left === right } &&
        (then_keyword_loc.nil? == other.then_keyword_loc.nil?) &&
        (statements === other.statements)
    end

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.

accept (visitor)
Source

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

def accept: (`Visitor` visitor) -> void

child_nodes ()
Source

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

def `child_nodes`: () -> Array

Also aliased as: deconstruct
comment_targets ()
Source

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

def `comment_targets`: () -> Array[Node | Location]

compact_child_nodes ()
Source

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

def `compact_child_nodes`: () -> Array

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)
Source

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

def copy: (?node_id: `Integer`, ?location: `Location`, ?flags: `Integer`, ?keyword_loc: `Location`, ?conditions: Array, ?then_keyword_loc: `Location`?, ?statements: `StatementsNode`?) -> `WhenNode`

deconstruct ()

def deconstruct: () -> Array

Alias for: child_nodes
deconstruct_keys (keys)
Source

    # File lib/prism/node.rb, line 18097
    def deconstruct_keys(keys)
      { node_id: node_id, location: location, keyword_loc: keyword_loc, conditions: conditions, then_keyword_loc: then_keyword_loc, statements: statements }
    end

def `deconstruct_keys`: (Array keys) -> { node_id: `Integer`, location: `Location`, `keyword_loc`: `Location`, conditions: Array, `then_keyword_loc`: `Location`?, statements: `StatementsNode`? }

inspect ()
Source

    # File lib/prism/node.rb, line 18150
    def inspect
      InspectVisitor.compose(self)
    end

def inspect -> `String`

keyword ()
Source

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

def keyword: () -> `String`

keyword_loc ()
Source

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

attr_reader `keyword_loc`: `Location`

save_keyword_loc (repository)
Source

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

then_keyword ()
Source

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

def `then_keyword`: () -> `String`?

then_keyword_loc ()
Source

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

attr_reader `then_keyword_loc`: `Location`?

type ()
Source

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

Return a symbol representation of this node type. See `Node#type`.