Class: Prism::BlockArgumentNode

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

Overview

Represents a block argument using ‘&`.

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, expression, operator_loc) ⇒ BlockArgumentNode

Initialize a new BlockArgumentNode node.



1648
1649
1650
1651
1652
1653
1654
1655
# File 'lib/prism/node.rb', line 1648

def initialize(source, node_id, location, flags, expression, operator_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @expression = expression
  @operator_loc = operator_loc
end

Instance Attribute Details

#expressionObject (readonly)

The expression that is being passed as a block argument. This can be any [non-void expression](github.com/ruby/prism/blob/main/docs/parsing_rules.md#non-void-expression).

foo(&args)
    ^^^^^


1696
1697
1698
# File 'lib/prism/node.rb', line 1696

def expression
  @expression
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See ‘Node::type`.



1730
1731
1732
# File 'lib/prism/node.rb', line 1730

def self.type
  :block_argument_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



1736
1737
1738
1739
1740
# File 'lib/prism/node.rb', line 1736

def ===(other)
  other.is_a?(BlockArgumentNode) &&
    (expression === other.expression) &&
    (operator_loc.nil? == other.operator_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



1658
1659
1660
# File 'lib/prism/node.rb', line 1658

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

#child_nodesObject Also known as: deconstruct

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



1663
1664
1665
# File 'lib/prism/node.rb', line 1663

def child_nodes
  [expression]
end

#comment_targetsObject

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



1675
1676
1677
# File 'lib/prism/node.rb', line 1675

def comment_targets
  [*expression, operator_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



1668
1669
1670
1671
1672
# File 'lib/prism/node.rb', line 1668

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << expression if expression
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, expression: self.expression, operator_loc: self.operator_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?expression: Prism::node?, ?operator_loc: Location) -> BlockArgumentNode



1680
1681
1682
# File 'lib/prism/node.rb', line 1680

def copy(node_id: self.node_id, location: self.location, flags: self.flags, expression: self.expression, operator_loc: self.operator_loc)
  BlockArgumentNode.new(source, node_id, location, flags, expression, operator_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, expression: Prism::node?, operator_loc: Location }



1688
1689
1690
# File 'lib/prism/node.rb', line 1688

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

#inspectObject

def inspect -> String



1720
1721
1722
# File 'lib/prism/node.rb', line 1720

def inspect
  InspectVisitor.compose(self)
end

#operatorObject

def operator: () -> String



1715
1716
1717
# File 'lib/prism/node.rb', line 1715

def operator
  operator_loc.slice
end

#operator_locObject

Represents the location of the ‘&` operator.

foo(&args)
    ^


1702
1703
1704
1705
1706
# File 'lib/prism/node.rb', line 1702

def operator_loc
  location = @operator_loc
  return location if location.is_a?(Location)
  @operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#save_operator_loc(repository) ⇒ Object

Save the operator_loc location using the given saved source so that it can be retrieved later.



1710
1711
1712
# File 'lib/prism/node.rb', line 1710

def save_operator_loc(repository)
  repository.enter(node_id, :operator_loc)
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



1725
1726
1727
# File 'lib/prism/node.rb', line 1725

def type
  :block_argument_node
end