Class: Prism::BlockParameterNode

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

Overview

Represents a block 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) ⇒ BlockParameterNode

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



1447
1448
1449
1450
1451
1452
# File 'lib/prism/node.rb', line 1447

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?



1438
1439
1440
# File 'lib/prism/node.rb', line 1438

def name
  @name
end

#name_locObject (readonly)

attr_reader name_loc: Location?



1441
1442
1443
# File 'lib/prism/node.rb', line 1441

def name_loc
  @name_loc
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



1444
1445
1446
# File 'lib/prism/node.rb', line 1444

def operator_loc
  @operator_loc
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



1455
1456
1457
# File 'lib/prism/node.rb', line 1455

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

#child_nodesObject Also known as: deconstruct

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



1460
1461
1462
# File 'lib/prism/node.rb', line 1460

def child_nodes
  []
end

#comment_targetsObject

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



1470
1471
1472
# File 'lib/prism/node.rb', line 1470

def comment_targets
  [*name_loc, operator_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



1465
1466
1467
# File 'lib/prism/node.rb', line 1465

def compact_child_nodes
  []
end

#copy(**params) ⇒ Object

def copy: (**params) -> BlockParameterNode



1475
1476
1477
1478
1479
1480
1481
1482
# File 'lib/prism/node.rb', line 1475

def copy(**params)
  BlockParameterNode.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[Node] | String | Token | Array[Token] | Location]



1488
1489
1490
# File 'lib/prism/node.rb', line 1488

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

#inspect(inspector = NodeInspector.new) ⇒ Object



1497
1498
1499
1500
1501
1502
1503
# File 'lib/prism/node.rb', line 1497

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



1493
1494
1495
# File 'lib/prism/node.rb', line 1493

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



1519
1520
1521
# File 'lib/prism/node.rb', line 1519

def type
  :block_parameter_node
end