Class: Prism::ClassVariableOrWriteNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::ClassVariableOrWriteNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents the use of the ‘||=` operator for assignment to a class variable.
@@target ||= value
^^^^^^^^^^^^^^^^^^
Instance Attribute Summary collapse
-
#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.
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) -> ClassVariableOrWriteNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(name, name_loc, operator_loc, value, location) ⇒ ClassVariableOrWriteNode
constructor
def initialize: (name: Symbol, name_loc: Location, operator_loc: Location, value: Node, location: Location) -> void.
- #inspect(inspector = NodeInspector.new) ⇒ Object
-
#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, name_loc, operator_loc, value, location) ⇒ ClassVariableOrWriteNode
def initialize: (name: Symbol, name_loc: Location, operator_loc: Location, value: Node, location: Location) -> void
3062 3063 3064 3065 3066 3067 3068 |
# File 'lib/prism/node.rb', line 3062 def initialize(name, name_loc, operator_loc, value, location) @name = name @name_loc = name_loc @operator_loc = operator_loc @value = value @location = location end |
Instance Attribute Details
#name ⇒ Object (readonly)
attr_reader name: Symbol
3050 3051 3052 |
# File 'lib/prism/node.rb', line 3050 def name @name end |
#name_loc ⇒ Object (readonly)
attr_reader name_loc: Location
3053 3054 3055 |
# File 'lib/prism/node.rb', line 3053 def name_loc @name_loc end |
#operator_loc ⇒ Object (readonly)
attr_reader operator_loc: Location
3056 3057 3058 |
# File 'lib/prism/node.rb', line 3056 def operator_loc @operator_loc end |
#value ⇒ Object (readonly)
attr_reader value: Node
3059 3060 3061 |
# File 'lib/prism/node.rb', line 3059 def value @value end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
3071 3072 3073 |
# File 'lib/prism/node.rb', line 3071 def accept(visitor) visitor.visit_class_variable_or_write_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
3076 3077 3078 |
# File 'lib/prism/node.rb', line 3076 def child_nodes [value] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
3086 3087 3088 |
# File 'lib/prism/node.rb', line 3086 def comment_targets [name_loc, operator_loc, value] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
3081 3082 3083 |
# File 'lib/prism/node.rb', line 3081 def compact_child_nodes [value] end |
#copy(**params) ⇒ Object
def copy: (**params) -> ClassVariableOrWriteNode
3091 3092 3093 3094 3095 3096 3097 3098 3099 |
# File 'lib/prism/node.rb', line 3091 def copy(**params) ClassVariableOrWriteNode.new( params.fetch(:name) { name }, params.fetch(:name_loc) { name_loc }, params.fetch(:operator_loc) { operator_loc }, params.fetch(:value) { value }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
3105 3106 3107 |
# File 'lib/prism/node.rb', line 3105 def deconstruct_keys(keys) { name: name, name_loc: name_loc, operator_loc: operator_loc, value: value, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
3114 3115 3116 3117 3118 3119 3120 3121 3122 |
# File 'lib/prism/node.rb', line 3114 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── name: #{name.inspect}\n" inspector << "├── name_loc: #{inspector.location(name_loc)}\n" inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n" inspector << "└── value:\n" inspector << inspector.child_node(value, " ") inspector.to_str end |
#operator ⇒ Object
def operator: () -> String
3110 3111 3112 |
# File 'lib/prism/node.rb', line 3110 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
3138 3139 3140 |
# File 'lib/prism/node.rb', line 3138 def type :class_variable_or_write_node end |