Class: Prism::BlockArgumentNode

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

Overview

Represents block method arguments.

bar(&args)
^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression, operator_loc, location) ⇒ BlockArgumentNode

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



1153
1154
1155
1156
1157
# File 'lib/prism/node.rb', line 1153

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

Instance Attribute Details

#expressionObject (readonly)

attr_reader expression: Node?



1147
1148
1149
# File 'lib/prism/node.rb', line 1147

def expression
  @expression
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



1150
1151
1152
# File 'lib/prism/node.rb', line 1150

def operator_loc
  @operator_loc
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



1160
1161
1162
# File 'lib/prism/node.rb', line 1160

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

#child_nodesObject Also known as: deconstruct

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



1165
1166
1167
# File 'lib/prism/node.rb', line 1165

def child_nodes
  [expression]
end

#comment_targetsObject

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



1177
1178
1179
# File 'lib/prism/node.rb', line 1177

def comment_targets
  [*expression, operator_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



1170
1171
1172
1173
1174
# File 'lib/prism/node.rb', line 1170

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

#copy(**params) ⇒ Object

def copy: (**params) -> BlockArgumentNode



1182
1183
1184
1185
1186
1187
1188
# File 'lib/prism/node.rb', line 1182

def copy(**params)
  BlockArgumentNode.new(
    params.fetch(:expression) { expression },
    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]



1194
1195
1196
# File 'lib/prism/node.rb', line 1194

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

#inspect(inspector = NodeInspector.new) ⇒ Object



1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
# File 'lib/prism/node.rb', line 1203

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

#operatorObject

def operator: () -> String



1199
1200
1201
# File 'lib/prism/node.rb', line 1199

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



1229
1230
1231
# File 'lib/prism/node.rb', line 1229

def type
  :block_argument_node
end