class Prism::AssocNode - Documentation for Ruby 4.1
Description: class Prism::AssocNode: value pair.
Keywords: ruby,class,Prism::AssocNode

🔍

No recent searches

# class Prism::AssocNode

Represents a hash key/value pair.

    { a => b }
      ^^^^^^

### Public Class Methods

new (source, node_id, location, flags, key, value, operator_loc)
Source

    # File lib/prism/node.rb, line 1892
    def initialize(source, node_id, location, flags, key, value, operator_loc)
      @source = source
      @node_id = node_id
      @location = location
      @flags = flags
      @key = key
      @value = value
      @operator_loc = operator_loc
    end

Initialize a new `AssocNode` node.

### Public Instance Methods

key → Node
Source

    # File lib/prism/node.rb, line 2005
    def key
      @key
    end

The key of the association. This can be any non-void expression.

    { a: b }
      ^

    { foo => bar }
      ^^^

    { def a; end => 1 }
      ^^^^^^^^^^

value → Node
Source

    # File lib/prism/node.rb, line 2021
    def value
      @value
    end

The value of the association, if present. This can be any non-void expression.

    { foo => bar }
             ^^^

    { x: 1 }
         ^

## Locations

### Public Instance Methods

operator_loc → Location | nil
Source

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

The `Location` of the `=>` operator, if present.

    { foo => bar }
          ^^

## 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 1979
    def self.type
      :assoc_node
    end

See `Node.type`.

### Public Instance Methods

accept (visitor)
Source

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

See `Node.accept`.

child_nodes ()
Source

    # File lib/prism/node.rb, line 1923
    def child_nodes
      [key, value]
    end

See `Node.child_nodes`.

Also aliased as: deconstruct
comment_targets ()
Source

    # File lib/prism/node.rb, line 1948
    def comment_targets
      [key, value, *operator_loc] #: Array[Prism::node | Location]
    end

See `Node.comment_targets`.

compact_child_nodes ()
Source

    # File lib/prism/node.rb, line 1941
    def compact_child_nodes
      [key, value]
    end

See `Node.compact_child_nodes`.

copy(**fields) → AssocNode
Source

    # File lib/prism/node.rb, line 1958
    def copy(node_id: self.node_id, location: self.location, flags: self.flags, key: self.key, value: self.value, operator_loc: self.operator_loc)
      AssocNode.new(source, node_id, location, flags, key, value, operator_loc)
    end

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

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

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

      yield key
      yield value
    end

See `Node.each_child_node`.

type ()
Source

    # File lib/prism/node.rb, line 1972
    def type
      :assoc_node
    end

See `Node#type`.

## Repository

Methods related to `Relocation`.

### Public Instance Methods

save_operator_loc (repository)
Source

    # File lib/prism/node.rb, line 2052
    def save_operator_loc(repository)
      repository.enter(node_id, :operator_loc) unless @operator_loc.nil?
    end

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

## Slicing

### Public Instance Methods

operator → String | nil
Source

    # File lib/prism/node.rb, line 2063
    def operator
      operator_loc&.slice
    end

Slice the location of `operator_loc` from the source.