Class: Prism::MultiTargetNode

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

Overview

Represents a multi-target expression.

a, b, c = 1, 2, 3
^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(targets, lparen_loc, rparen_loc, location) ⇒ MultiTargetNode

def initialize: (targets: Array, lparen_loc: Location?, rparen_loc: Location?, location: Location) -> void



10017
10018
10019
10020
10021
10022
# File 'lib/prism/node.rb', line 10017

def initialize(targets, lparen_loc, rparen_loc, location)
  @targets = targets
  @lparen_loc = lparen_loc
  @rparen_loc = rparen_loc
  @location = location
end

Instance Attribute Details

#lparen_locObject (readonly)

attr_reader lparen_loc: Location?



10011
10012
10013
# File 'lib/prism/node.rb', line 10011

def lparen_loc
  @lparen_loc
end

#rparen_locObject (readonly)

attr_reader rparen_loc: Location?



10014
10015
10016
# File 'lib/prism/node.rb', line 10014

def rparen_loc
  @rparen_loc
end

#targetsObject (readonly)

attr_reader targets: Array



10008
10009
10010
# File 'lib/prism/node.rb', line 10008

def targets
  @targets
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



10025
10026
10027
# File 'lib/prism/node.rb', line 10025

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

#child_nodesObject Also known as: deconstruct

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



10030
10031
10032
# File 'lib/prism/node.rb', line 10030

def child_nodes
  [*targets]
end

#comment_targetsObject

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



10040
10041
10042
# File 'lib/prism/node.rb', line 10040

def comment_targets
  [*targets, *lparen_loc, *rparen_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



10035
10036
10037
# File 'lib/prism/node.rb', line 10035

def compact_child_nodes
  [*targets]
end

#copy(**params) ⇒ Object

def copy: (**params) -> MultiTargetNode



10045
10046
10047
10048
10049
10050
10051
10052
# File 'lib/prism/node.rb', line 10045

def copy(**params)
  MultiTargetNode.new(
    params.fetch(:targets) { targets },
    params.fetch(:lparen_loc) { lparen_loc },
    params.fetch(:rparen_loc) { rparen_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



10058
10059
10060
# File 'lib/prism/node.rb', line 10058

def deconstruct_keys(keys)
  { targets: targets, lparen_loc: lparen_loc, rparen_loc: rparen_loc, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



10072
10073
10074
10075
10076
10077
10078
# File 'lib/prism/node.rb', line 10072

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── targets: #{inspector.list("#{inspector.prefix}│   ", targets)}"
  inspector << "├── lparen_loc: #{inspector.location(lparen_loc)}\n"
  inspector << "└── rparen_loc: #{inspector.location(rparen_loc)}\n"
  inspector.to_str
end

#lparenObject

def lparen: () -> String?



10063
10064
10065
# File 'lib/prism/node.rb', line 10063

def lparen
  lparen_loc&.slice
end

#rparenObject

def rparen: () -> String?



10068
10069
10070
# File 'lib/prism/node.rb', line 10068

def rparen
  rparen_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



10094
10095
10096
# File 'lib/prism/node.rb', line 10094

def type
  :multi_target_node
end