Class: Prism::LocalVariableOrWriteNode

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 local variable.

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

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize a new LocalVariableOrWriteNode node.

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

Parameters:

  • source (Source)
  • node_id (Integer)
  • location (Location)
  • flags (Integer)
  • name_loc (Location)
  • operator_loc (Location)
  • value (Prism::node)
  • name (Symbol)
  • depth (Integer)


21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
# File 'lib/prism/node.rb', line 21839

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

Class Method Details

.type ⇒ :local_variable_or_write_node

See Node.type.

: () -> :local_variable_or_write_node

Returns:

  • (:local_variable_or_write_node)


21933
21934
21935
# File 'lib/prism/node.rb', line 21933

def self.type
  :local_variable_or_write_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


22033
22034
22035
22036
22037
22038
22039
22040
# File 'lib/prism/node.rb', line 22033

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

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


21865
21866
21867
# File 'lib/prism/node.rb', line 21865

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

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

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


21872
21873
21874
# File 'lib/prism/node.rb', line 21872

def child_nodes
  [value]
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


21889
21890
21891
# File 'lib/prism/node.rb', line 21889

def compact_child_nodes
  [value]
end

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

:call-seq:

copy(**fields) -> LocalVariableOrWriteNode

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

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

Parameters:

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

Returns:



21899
21900
21901
# File 'lib/prism/node.rb', line 21899

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

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

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

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
# File 'lib/prism/node.rb', line 21906

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location name_loc operator_loc value name depth 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_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 :name                    then deconstructed[:name] = self.name
    when :depth                   then deconstructed[:depth] = self.depth
    when :operator                then deconstructed[:operator] = self.operator
    end
  end
end

#depth ⇒ Integer

:call-seq:

depth -> Integer

Returns the depth attribute.

: () -> Integer

Returns:

  • (Integer)


22014
22015
22016
# File 'lib/prism/node.rb', line 22014

def depth
  @depth
end

#desugar ⇒ node

: () -> node

Returns:

  • (node)


283
284
285
# File 'lib/prism/desugar_compiler.rb', line 283

def desugar # :nodoc:
  DesugarOrWriteNode.new(self, source, :local_variable_read_node, :local_variable_write_node, name: name, depth: depth).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)


21880
21881
21882
21883
21884
# File 'lib/prism/node.rb', line 21880

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

  yield value
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


21938
21939
21940
# File 'lib/prism/node.rb', line 21938

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

#name ⇒ Symbol

:call-seq:

name -> Symbol

Returns the name attribute.

: () -> Symbol

Returns:

  • (Symbol)


22004
22005
22006
# File 'lib/prism/node.rb', line 22004

def name
  @name
end

#name_loc ⇒ Location

:category: Locations :call-seq:

name_loc -> Location

Returns the Location represented by name_loc.

: () -> Location

Returns:



21951
21952
21953
21954
21955
# File 'lib/prism/node.rb', line 21951

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)


22026
22027
22028
# File 'lib/prism/node.rb', line 22026

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:



21973
21974
21975
21976
21977
# File 'lib/prism/node.rb', line 21973

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:



21962
21963
21964
# File 'lib/prism/node.rb', line 21962

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:



21984
21985
21986
# File 'lib/prism/node.rb', line 21984

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

#type ⇒ :local_variable_or_write_node

See Node#type.

: () -> :local_variable_or_write_node

Returns:

  • (:local_variable_or_write_node)


21926
21927
21928
# File 'lib/prism/node.rb', line 21926

def type
  :local_variable_or_write_node
end

#value ⇒ Prism::node

:call-seq:

value -> Node

Returns the value attribute.

: () -> Prism::node

Returns:

  • (Prism::node)


21994
21995
21996
# File 'lib/prism/node.rb', line 21994

def value
  @value
end