Class: Prism::RestParameterNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::RestParameterNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents a rest parameter to a method, block, or lambda definition.
def a(*b)
^^
end
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.
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) -> RestParameterNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(name, name_loc, operator_loc, location) ⇒ RestParameterNode
constructor
def initialize: (name: Symbol?, name_loc: Location?, 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, name_loc, operator_loc, location) ⇒ RestParameterNode
def initialize: (name: Symbol?, name_loc: Location?, operator_loc: Location, location: Location) -> void
14922 14923 14924 14925 14926 14927 |
# File 'lib/prism/node.rb', line 14922 def initialize(name, name_loc, operator_loc, location) @name = name @name_loc = name_loc @operator_loc = operator_loc @location = location end |
Instance Attribute Details
#name ⇒ Object (readonly)
attr_reader name: Symbol?
14913 14914 14915 |
# File 'lib/prism/node.rb', line 14913 def name @name end |
#name_loc ⇒ Object (readonly)
attr_reader name_loc: Location?
14916 14917 14918 |
# File 'lib/prism/node.rb', line 14916 def name_loc @name_loc end |
#operator_loc ⇒ Object (readonly)
attr_reader operator_loc: Location
14919 14920 14921 |
# File 'lib/prism/node.rb', line 14919 def operator_loc @operator_loc 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
15009 15010 15011 |
# File 'lib/prism/node.rb', line 15009 def self.type :rest_parameter_node end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
14930 14931 14932 |
# File 'lib/prism/node.rb', line 14930 def accept(visitor) visitor.visit_rest_parameter_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
14935 14936 14937 |
# File 'lib/prism/node.rb', line 14935 def child_nodes [] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
14945 14946 14947 |
# File 'lib/prism/node.rb', line 14945 def comment_targets [*name_loc, operator_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
14940 14941 14942 |
# File 'lib/prism/node.rb', line 14940 def compact_child_nodes [] end |
#copy(**params) ⇒ Object
def copy: (**params) -> RestParameterNode
14950 14951 14952 14953 14954 14955 14956 14957 |
# File 'lib/prism/node.rb', line 14950 def copy(**params) RestParameterNode.new( params.fetch(:name) { name }, params.fetch(:name_loc) { name_loc }, params.fetch(:operator_loc) { operator_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
14963 14964 14965 |
# File 'lib/prism/node.rb', line 14963 def deconstruct_keys(keys) { name: name, name_loc: name_loc, operator_loc: operator_loc, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String
14973 14974 14975 14976 14977 14978 14979 14980 14981 14982 14983 |
# File 'lib/prism/node.rb', line 14973 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) if (name = self.name).nil? inspector << "├── name: ∅\n" else inspector << "├── name: #{name.inspect}\n" end inspector << "├── name_loc: #{inspector.location(name_loc)}\n" inspector << "└── operator_loc: #{inspector.location(operator_loc)}\n" inspector.to_str end |
#operator ⇒ Object
def operator: () -> String
14968 14969 14970 |
# File 'lib/prism/node.rb', line 14968 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
14999 15000 15001 |
# File 'lib/prism/node.rb', line 14999 def type :rest_parameter_node end |