Class: Prism::KeywordRestParameterNode

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

Overview

Represents a keyword rest parameter to a method, block, or lambda definition.

def a(**b)
      ^^^
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, name_loc, operator_loc, location) ⇒ KeywordRestParameterNode

def initialize: (name: Symbol?, name_loc: Location?, operator_loc: Location, location: Location) -> void



8588
8589
8590
8591
8592
8593
# File 'lib/prism/node.rb', line 8588

def initialize(name, name_loc, operator_loc, location)
  @name = name
  @name_loc = name_loc
  @operator_loc = operator_loc
  @location = location
end

Instance Attribute Details

#nameObject (readonly)

attr_reader name: Symbol?



8579
8580
8581
# File 'lib/prism/node.rb', line 8579

def name
  @name
end

#name_locObject (readonly)

attr_reader name_loc: Location?



8582
8583
8584
# File 'lib/prism/node.rb', line 8582

def name_loc
  @name_loc
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



8585
8586
8587
# File 'lib/prism/node.rb', line 8585

def operator_loc
  @operator_loc
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



8596
8597
8598
# File 'lib/prism/node.rb', line 8596

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

#child_nodesObject Also known as: deconstruct

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



8601
8602
8603
# File 'lib/prism/node.rb', line 8601

def child_nodes
  []
end

#comment_targetsObject

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



8611
8612
8613
# File 'lib/prism/node.rb', line 8611

def comment_targets
  [*name_loc, operator_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



8606
8607
8608
# File 'lib/prism/node.rb', line 8606

def compact_child_nodes
  []
end

#copy(**params) ⇒ Object

def copy: (**params) -> KeywordRestParameterNode



8616
8617
8618
8619
8620
8621
8622
8623
# File 'lib/prism/node.rb', line 8616

def copy(**params)
  KeywordRestParameterNode.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

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



8629
8630
8631
# File 'lib/prism/node.rb', line 8629

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

#inspect(inspector = NodeInspector.new) ⇒ Object



8638
8639
8640
8641
8642
8643
8644
# File 'lib/prism/node.rb', line 8638

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.to_str
end

#operatorObject

def operator: () -> String



8634
8635
8636
# File 'lib/prism/node.rb', line 8634

def operator
  operator_loc.slice
end

#typeObject

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



8660
8661
8662
# File 'lib/prism/node.rb', line 8660

def type
  :keyword_rest_parameter_node
end