Class: Prism::GlobalVariableWriteNode

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

Overview

Represents writing to a global variable.

$foo = 1
^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize a new GlobalVariableWriteNode node.

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

Parameters:



14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
# File 'lib/prism/node.rb', line 14841

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

Class Method Details

.type ⇒ :global_variable_write_node

See Node.type.

: () -> :global_variable_write_node

Returns:

  • (:global_variable_write_node)


14933
14934
14935
# File 'lib/prism/node.rb', line 14933

def self.type
  :global_variable_write_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


15055
15056
15057
15058
15059
15060
15061
# File 'lib/prism/node.rb', line 15055

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

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


14866
14867
14868
# File 'lib/prism/node.rb', line 14866

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

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

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


14873
14874
14875
# File 'lib/prism/node.rb', line 14873

def child_nodes
  [value]
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


14890
14891
14892
# File 'lib/prism/node.rb', line 14890

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, value: self.value, operator_loc: self.operator_loc) ⇒ GlobalVariableWriteNode

:call-seq:

copy(**fields) -> GlobalVariableWriteNode

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, ?value: Prism::node, ?operator_loc: Location) -> GlobalVariableWriteNode

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)
  • value: (Prism::node) (defaults to: self.value)
  • operator_loc: (Location) (defaults to: self.operator_loc)

Returns:



14900
14901
14902
# File 'lib/prism/node.rb', line 14900

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

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

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

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
# File 'lib/prism/node.rb', line 14907

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


14881
14882
14883
14884
14885
# File 'lib/prism/node.rb', line 14881

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

  yield value
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


14938
14939
14940
# File 'lib/prism/node.rb', line 14938

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

#name ⇒ Symbol

:call-seq:

name -> Symbol

The name of the global variable, which is a $ followed by an identifier. Alternatively, it can be one of the special global variables designated by a symbol.

$foo = :bar  # name `:$foo`

$_Test = 123 # name `:$_Test`

Explore code sample

: () -> Symbol

Returns:

  • (Symbol)


14958
14959
14960
# File 'lib/prism/node.rb', line 14958

def name
  @name
end

#name_loc ⇒ Location

:category: Locations :call-seq:

name_loc -> Location

The Location of the global variable's name.

$foo = :bar
^^^^

Explore code sample

: () -> Location

Returns:



14976
14977
14978
14979
14980
# File 'lib/prism/node.rb', line 14976

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

#newline_flag!(lines) ⇒ void

This method returns an undefined value.

: (Array lines) -> void

Parameters:

  • lines (Array[bool])


319
320
321
# File 'lib/prism/parse_result/newlines.rb', line 319

def newline_flag!(lines) # :nodoc:
  value.newline_flag!(lines)
end

#operator ⇒ String

:call-seq:

operator -> String

Slice the location of operator_loc from the source.

: () -> String

Returns:

  • (String)


15048
15049
15050
# File 'lib/prism/node.rb', line 15048

def operator
  operator_loc.slice
end

#operator_loc ⇒ Location

:category: Locations :call-seq:

operator_loc -> Location

The Location of the = operator.

$foo = :bar
     ^

Explore code sample

: () -> Location

Returns:



15025
15026
15027
15028
15029
# File 'lib/prism/node.rb', line 15025

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:



14987
14988
14989
# File 'lib/prism/node.rb', line 14987

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:



15036
15037
15038
# File 'lib/prism/node.rb', line 15036

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

#type ⇒ :global_variable_write_node

See Node#type.

: () -> :global_variable_write_node

Returns:

  • (:global_variable_write_node)


14926
14927
14928
# File 'lib/prism/node.rb', line 14926

def type
  :global_variable_write_node
end

#value ⇒ Prism::node

:call-seq:

value -> Node

The value to write to the global variable. It can be any non-void expression.

$foo = :bar
       ^^^^

$-xyz = 123
        ^^^

Explore code sample

: () -> Prism::node

Returns:

  • (Prism::node)


15007
15008
15009
# File 'lib/prism/node.rb', line 15007

def value
  @value
end