Class: Prism::LocalVariableWriteNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::LocalVariableWriteNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents writing to a local variable.
foo = 1
^^^^^^^
Instance Attribute Summary collapse
-
#depth ⇒ Object
readonly
attr_reader depth: Integer.
-
#name ⇒ Object
readonly
attr_reader name: Symbol.
-
#name_loc ⇒ Object
readonly
attr_reader name_loc: Location.
-
#operator_loc ⇒ Object
readonly
attr_reader operator_loc: Location.
-
#value ⇒ Object
readonly
attr_reader value: Node.
Class Method Summary collapse
-
.type ⇒ Object
Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#copy(**params) ⇒ Object
def copy: (**params) -> LocalVariableWriteNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(name, depth, name_loc, value, operator_loc, location) ⇒ LocalVariableWriteNode
constructor
def initialize: (name: Symbol, depth: Integer, name_loc: Location, value: Node, operator_loc: Location, location: Location) -> void.
-
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String.
-
#operator ⇒ Object
def operator: () -> String.
-
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform.
Constructor Details
#initialize(name, depth, name_loc, value, operator_loc, location) ⇒ LocalVariableWriteNode
def initialize: (name: Symbol, depth: Integer, name_loc: Location, value: Node, operator_loc: Location, location: Location) -> void
11311 11312 11313 11314 11315 11316 11317 11318 |
# File 'lib/prism/node.rb', line 11311 def initialize(name, depth, name_loc, value, operator_loc, location) @name = name @depth = depth @name_loc = name_loc @value = value @operator_loc = operator_loc @location = location end |
Instance Attribute Details
#depth ⇒ Object (readonly)
attr_reader depth: Integer
11299 11300 11301 |
# File 'lib/prism/node.rb', line 11299 def depth @depth end |
#name ⇒ Object (readonly)
attr_reader name: Symbol
11296 11297 11298 |
# File 'lib/prism/node.rb', line 11296 def name @name end |
#name_loc ⇒ Object (readonly)
attr_reader name_loc: Location
11302 11303 11304 |
# File 'lib/prism/node.rb', line 11302 def name_loc @name_loc end |
#operator_loc ⇒ Object (readonly)
attr_reader operator_loc: Location
11308 11309 11310 |
# File 'lib/prism/node.rb', line 11308 def operator_loc @operator_loc end |
#value ⇒ Object (readonly)
attr_reader value: Node
11305 11306 11307 |
# File 'lib/prism/node.rb', line 11305 def value @value end |
Class Method Details
.type ⇒ Object
Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.
def self.type: () -> Symbol
11401 11402 11403 |
# File 'lib/prism/node.rb', line 11401 def self.type :local_variable_write_node end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
11321 11322 11323 |
# File 'lib/prism/node.rb', line 11321 def accept(visitor) visitor.visit_local_variable_write_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
11326 11327 11328 |
# File 'lib/prism/node.rb', line 11326 def child_nodes [value] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
11336 11337 11338 |
# File 'lib/prism/node.rb', line 11336 def comment_targets [name_loc, value, operator_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
11331 11332 11333 |
# File 'lib/prism/node.rb', line 11331 def compact_child_nodes [value] end |
#copy(**params) ⇒ Object
def copy: (**params) -> LocalVariableWriteNode
11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 |
# File 'lib/prism/node.rb', line 11341 def copy(**params) LocalVariableWriteNode.new( params.fetch(:name) { name }, params.fetch(:depth) { depth }, params.fetch(:name_loc) { name_loc }, params.fetch(:value) { value }, params.fetch(:operator_loc) { operator_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
11356 11357 11358 |
# File 'lib/prism/node.rb', line 11356 def deconstruct_keys(keys) { name: name, depth: depth, name_loc: name_loc, value: value, operator_loc: operator_loc, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String
11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 |
# File 'lib/prism/node.rb', line 11366 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── name: #{name.inspect}\n" inspector << "├── depth: #{depth.inspect}\n" inspector << "├── name_loc: #{inspector.location(name_loc)}\n" inspector << "├── value:\n" inspector << inspector.child_node(value, "│ ") inspector << "└── operator_loc: #{inspector.location(operator_loc)}\n" inspector.to_str end |
#operator ⇒ Object
def operator: () -> String
11361 11362 11363 |
# File 'lib/prism/node.rb', line 11361 def operator operator_loc.slice end |
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.
Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.
def type: () -> Symbol
11391 11392 11393 |
# File 'lib/prism/node.rb', line 11391 def type :local_variable_write_node end |