Class: Prism::SplatNode

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

Overview

Represents the use of the splat operator.

[*a]
 ^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator_loc, expression, location) ⇒ SplatNode

def initialize: (operator_loc: Location, expression: Node?, location: Location) -> void



12998
12999
13000
13001
13002
# File 'lib/prism/node.rb', line 12998

def initialize(operator_loc, expression, location)
  @operator_loc = operator_loc
  @expression = expression
  @location = location
end

Instance Attribute Details

#expressionObject (readonly)

attr_reader expression: Node?



12995
12996
12997
# File 'lib/prism/node.rb', line 12995

def expression
  @expression
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



12992
12993
12994
# File 'lib/prism/node.rb', line 12992

def operator_loc
  @operator_loc
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



13005
13006
13007
# File 'lib/prism/node.rb', line 13005

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

#child_nodesObject Also known as: deconstruct

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



13010
13011
13012
# File 'lib/prism/node.rb', line 13010

def child_nodes
  [expression]
end

#comment_targetsObject

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



13022
13023
13024
# File 'lib/prism/node.rb', line 13022

def comment_targets
  [operator_loc, *expression]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



13015
13016
13017
13018
13019
# File 'lib/prism/node.rb', line 13015

def compact_child_nodes
  compact = []
  compact << expression if expression
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> SplatNode



13027
13028
13029
13030
13031
13032
13033
# File 'lib/prism/node.rb', line 13027

def copy(**params)
  SplatNode.new(
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:expression) { expression },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



13039
13040
13041
# File 'lib/prism/node.rb', line 13039

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

#inspect(inspector = NodeInspector.new) ⇒ Object



13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
# File 'lib/prism/node.rb', line 13048

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n"
  if (expression = self.expression).nil?
    inspector << "└── expression: ∅\n"
  else
    inspector << "└── expression:\n"
    inspector << expression.inspect(inspector.child_inspector("    ")).delete_prefix(inspector.prefix)
  end
  inspector.to_str
end

#operatorObject

def operator: () -> String



13044
13045
13046
# File 'lib/prism/node.rb', line 13044

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



13074
13075
13076
# File 'lib/prism/node.rb', line 13074

def type
  :splat_node
end