Class: Prism::InstanceVariableWriteNode

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

Overview

Represents writing to an instance variable.

@foo = 1
^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize a new InstanceVariableWriteNode node.



9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
# File 'lib/prism/node.rb', line 9107

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

Instance Attribute Details

#nameObject (readonly)

The name of the instance variable, which is a ‘@` followed by an [identifier](github.com/ruby/prism/blob/main/docs/parsing_rules.md#identifiers).

@x = :y       # name `:@x`

@_foo = "bar" # name `@_foo`


9156
9157
9158
# File 'lib/prism/node.rb', line 9156

def name
  @name
end

#valueObject (readonly)

The value to write to the instance variable. It can be any [non-void expression](github.com/ruby/prism/blob/main/docs/parsing_rules.md#non-void-expression).

@foo = :bar
       ^^^^

@_x = 1234
      ^^^^


9175
9176
9177
# File 'lib/prism/node.rb', line 9175

def value
  @value
end

Class Method Details

.typeObject

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



9203
9204
9205
# File 'lib/prism/node.rb', line 9203

def self.type
  :instance_variable_write_node
end

Instance Method Details

#===(other) ⇒ Object

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



9209
9210
9211
9212
9213
9214
9215
# File 'lib/prism/node.rb', line 9209

def ===(other)
  other.is_a?(InstanceVariableWriteNode) &&
    (name === other.name) &&
    (name_loc.nil? == other.name_loc.nil?) &&
    (value === other.value) &&
    (operator_loc.nil? == other.operator_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



9119
9120
9121
# File 'lib/prism/node.rb', line 9119

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



9124
9125
9126
# File 'lib/prism/node.rb', line 9124

def child_nodes
  [value]
end

#comment_targetsObject

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



9134
9135
9136
# File 'lib/prism/node.rb', line 9134

def comment_targets
  [name_loc, value, operator_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



9129
9130
9131
# File 'lib/prism/node.rb', line 9129

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) ⇒ Object

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



9139
9140
9141
# File 'lib/prism/node.rb', line 9139

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)
  InstanceVariableWriteNode.new(source, node_id, location, flags, name, name_loc, value, operator_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, name: Symbol, name_loc: Location, value: Prism::node, operator_loc: Location }



9147
9148
9149
# File 'lib/prism/node.rb', line 9147

def deconstruct_keys(keys)
  { node_id: node_id, location: location, name: name, name_loc: name_loc, value: value, operator_loc: operator_loc }
end

#inspectObject

def inspect -> String



9193
9194
9195
# File 'lib/prism/node.rb', line 9193

def inspect
  InspectVisitor.compose(self)
end

#name_locObject

The location of the variable name.

@_x = 1
^^^


9162
9163
9164
9165
9166
# File 'lib/prism/node.rb', line 9162

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

#operatorObject

def operator: () -> String



9188
9189
9190
# File 'lib/prism/node.rb', line 9188

def operator
  operator_loc.slice
end

#operator_locObject

The location of the ‘=` operator.

@x = y
   ^


9181
9182
9183
9184
9185
# File 'lib/prism/node.rb', line 9181

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

#typeObject

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



9198
9199
9200
# File 'lib/prism/node.rb', line 9198

def type
  :instance_variable_write_node
end