Class: Prism::ClassVariableOrWriteNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
lib/prism/desugar_compiler.rb,
sig/generated/prism/node.rbs,
sig/generated/prism/desugar_compiler.rbs,
ext/prism/api_node.c

Overview

Represents the use of the ||= operator for assignment to a class variable.

@@target ||= value
^^^^^^^^^^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, name, name_loc, operator_loc, value) ⇒ ClassVariableOrWriteNode

Initialize a new ClassVariableOrWriteNode node.

: (Source source, Integer node_id, Location location, Integer flags, Symbol name, Location name_loc, Location operator_loc, Prism::node value) -> void

Parameters:



7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
# File 'lib/prism/node.rb', line 7677

def initialize(source, node_id, location, flags, name, name_loc, operator_loc, value)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @name = name
  @name_loc = name_loc
  @operator_loc = operator_loc
  @value = value
end

Class Method Details

.type ⇒ :class_variable_or_write_node

See Node.type.

: () -> :class_variable_or_write_node

Returns:

  • (:class_variable_or_write_node)


7769
7770
7771
# File 'lib/prism/node.rb', line 7769

def self.type
  :class_variable_or_write_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


7859
7860
7861
7862
7863
7864
7865
# File 'lib/prism/node.rb', line 7859

def ===(other) # :nodoc:
  other.is_a?(ClassVariableOrWriteNode) &&
    (name === other.name) &&
    (name_loc.nil? == other.name_loc.nil?) &&
    (operator_loc.nil? == other.operator_loc.nil?) &&
    (value === other.value)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


7702
7703
7704
# File 'lib/prism/node.rb', line 7702

def accept(visitor)
  visitor.visit_class_variable_or_write_node(self)
end

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


7709
7710
7711
# File 'lib/prism/node.rb', line 7709

def child_nodes
  [value]
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


7726
7727
7728
# File 'lib/prism/node.rb', line 7726

def compact_child_nodes
  [value]
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, name: self.name, name_loc: self.name_loc, operator_loc: self.operator_loc, value: self.value) ⇒ ClassVariableOrWriteNode

:call-seq:

copy(**fields) -> ClassVariableOrWriteNode

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

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?name: Symbol, ?name_loc: Location, ?operator_loc: Location, ?value: Prism::node) -> ClassVariableOrWriteNode

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • name: (Symbol) (defaults to: self.name)
  • name_loc: (Location) (defaults to: self.name_loc)
  • operator_loc: (Location) (defaults to: self.operator_loc)
  • value: (Prism::node) (defaults to: self.value)

Returns:



7736
7737
7738
# File 'lib/prism/node.rb', line 7736

def copy(node_id: self.node_id, location: self.location, flags: self.flags, name: self.name, name_loc: self.name_loc, operator_loc: self.operator_loc, value: self.value)
  ClassVariableOrWriteNode.new(source, node_id, location, flags, name, name_loc, operator_loc, value)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
# File 'lib/prism/node.rb', line 7743

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location name name_loc operator_loc value operator]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :name                    then deconstructed[:name] = self.name
    when :name_loc                then deconstructed[:name_loc] = self.name_loc
    when :operator_loc            then deconstructed[:operator_loc] = self.operator_loc
    when :value                   then deconstructed[:value] = self.value
    when :operator                then deconstructed[:operator] = self.operator
    end
  end
end

#desugar ⇒ node

: () -> node

Returns:

  • (node)


199
200
201
# File 'lib/prism/desugar_compiler.rb', line 199

def desugar # :nodoc:
  DesugarOrWriteDefinedNode.new(self, source, :class_variable_read_node, :class_variable_write_node, name: name).compile
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


7717
7718
7719
7720
7721
# File 'lib/prism/node.rb', line 7717

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  yield value
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


7774
7775
7776
# File 'lib/prism/node.rb', line 7774

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#name ⇒ Symbol

:call-seq:

name -> Symbol

Returns the name attribute.

: () -> Symbol

Returns:

  • (Symbol)


7786
7787
7788
# File 'lib/prism/node.rb', line 7786

def name
  @name
end

#name_loc ⇒ Location

:category: Locations :call-seq:

name_loc -> Location

Returns the Location represented by name_loc.

: () -> Location

Returns:



7797
7798
7799
7800
7801
# File 'lib/prism/node.rb', line 7797

def name_loc
  location = @name_loc
  return location if location.is_a?(Location)
  @name_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#operator ⇒ String

:call-seq:

operator -> String

Slice the location of operator_loc from the source.

: () -> String

Returns:

  • (String)


7852
7853
7854
# File 'lib/prism/node.rb', line 7852

def operator
  operator_loc.slice
end

#operator_loc ⇒ Location

:category: Locations :call-seq:

operator_loc -> Location

Returns the Location represented by operator_loc.

: () -> Location

Returns:



7819
7820
7821
7822
7823
# File 'lib/prism/node.rb', line 7819

def operator_loc
  location = @operator_loc
  return location if location.is_a?(Location)
  @operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#save_name_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the name_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



7808
7809
7810
# File 'lib/prism/node.rb', line 7808

def save_name_loc(repository)
  repository.enter(node_id, :name_loc)
end

#save_operator_loc(repository) ⇒ Relocation::Entry

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

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



7830
7831
7832
# File 'lib/prism/node.rb', line 7830

def save_operator_loc(repository)
  repository.enter(node_id, :operator_loc)
end

#type ⇒ :class_variable_or_write_node

See Node#type.

: () -> :class_variable_or_write_node

Returns:

  • (:class_variable_or_write_node)


7762
7763
7764
# File 'lib/prism/node.rb', line 7762

def type
  :class_variable_or_write_node
end

#value ⇒ Prism::node

:call-seq:

value -> Node

Returns the value attribute.

: () -> Prism::node

Returns:

  • (Prism::node)


7840
7841
7842
# File 'lib/prism/node.rb', line 7840

def value
  @value
end