Class: Prism::ForNode

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

Overview

Represents the use of the ‘for` keyword.

for i in a end
^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_loc, end_keyword_loc, location) ⇒ ForNode

def initialize: (index: Node, collection: Node, statements: StatementsNode?, for_keyword_loc: Location, in_keyword_loc: Location, do_keyword_loc: Location?, end_keyword_loc: Location, location: Location) -> void



5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
# File 'lib/prism/node.rb', line 5642

def initialize(index, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_loc, end_keyword_loc, location)
  @index = index
  @collection = collection
  @statements = statements
  @for_keyword_loc = for_keyword_loc
  @in_keyword_loc = in_keyword_loc
  @do_keyword_loc = do_keyword_loc
  @end_keyword_loc = end_keyword_loc
  @location = location
end

Instance Attribute Details

#collectionObject (readonly)

attr_reader collection: Node



5624
5625
5626
# File 'lib/prism/node.rb', line 5624

def collection
  @collection
end

#do_keyword_locObject (readonly)

attr_reader do_keyword_loc: Location?



5636
5637
5638
# File 'lib/prism/node.rb', line 5636

def do_keyword_loc
  @do_keyword_loc
end

#end_keyword_locObject (readonly)

attr_reader end_keyword_loc: Location



5639
5640
5641
# File 'lib/prism/node.rb', line 5639

def end_keyword_loc
  @end_keyword_loc
end

#for_keyword_locObject (readonly)

attr_reader for_keyword_loc: Location



5630
5631
5632
# File 'lib/prism/node.rb', line 5630

def for_keyword_loc
  @for_keyword_loc
end

#in_keyword_locObject (readonly)

attr_reader in_keyword_loc: Location



5633
5634
5635
# File 'lib/prism/node.rb', line 5633

def in_keyword_loc
  @in_keyword_loc
end

#indexObject (readonly)

attr_reader index: Node



5621
5622
5623
# File 'lib/prism/node.rb', line 5621

def index
  @index
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



5627
5628
5629
# File 'lib/prism/node.rb', line 5627

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



5654
5655
5656
# File 'lib/prism/node.rb', line 5654

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

#child_nodesObject Also known as: deconstruct

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



5659
5660
5661
# File 'lib/prism/node.rb', line 5659

def child_nodes
  [index, collection, statements]
end

#comment_targetsObject

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



5673
5674
5675
# File 'lib/prism/node.rb', line 5673

def comment_targets
  [index, collection, *statements, for_keyword_loc, in_keyword_loc, *do_keyword_loc, end_keyword_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



5664
5665
5666
5667
5668
5669
5670
# File 'lib/prism/node.rb', line 5664

def compact_child_nodes
  compact = []
  compact << index
  compact << collection
  compact << statements if statements
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> ForNode



5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
# File 'lib/prism/node.rb', line 5678

def copy(**params)
  ForNode.new(
    params.fetch(:index) { index },
    params.fetch(:collection) { collection },
    params.fetch(:statements) { statements },
    params.fetch(:for_keyword_loc) { for_keyword_loc },
    params.fetch(:in_keyword_loc) { in_keyword_loc },
    params.fetch(:do_keyword_loc) { do_keyword_loc },
    params.fetch(:end_keyword_loc) { end_keyword_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



5695
5696
5697
# File 'lib/prism/node.rb', line 5695

def deconstruct_keys(keys)
  { index: index, collection: collection, statements: statements, for_keyword_loc: for_keyword_loc, in_keyword_loc: in_keyword_loc, do_keyword_loc: do_keyword_loc, end_keyword_loc: end_keyword_loc, location: location }
end

#do_keywordObject

def do_keyword: () -> String?



5710
5711
5712
# File 'lib/prism/node.rb', line 5710

def do_keyword
  do_keyword_loc&.slice
end

#end_keywordObject

def end_keyword: () -> String



5715
5716
5717
# File 'lib/prism/node.rb', line 5715

def end_keyword
  end_keyword_loc.slice
end

#for_keywordObject

def for_keyword: () -> String



5700
5701
5702
# File 'lib/prism/node.rb', line 5700

def for_keyword
  for_keyword_loc.slice
end

#in_keywordObject

def in_keyword: () -> String



5705
5706
5707
# File 'lib/prism/node.rb', line 5705

def in_keyword
  in_keyword_loc.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object



5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
# File 'lib/prism/node.rb', line 5719

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── index:\n"
  inspector << inspector.child_node(index, "│   ")
  inspector << "├── collection:\n"
  inspector << inspector.child_node(collection, "│   ")
  if (statements = self.statements).nil?
    inspector << "├── statements: ∅\n"
  else
    inspector << "├── statements:\n"
    inspector << statements.inspect(inspector.child_inspector("│   ")).delete_prefix(inspector.prefix)
  end
  inspector << "├── for_keyword_loc: #{inspector.location(for_keyword_loc)}\n"
  inspector << "├── in_keyword_loc: #{inspector.location(in_keyword_loc)}\n"
  inspector << "├── do_keyword_loc: #{inspector.location(do_keyword_loc)}\n"
  inspector << "└── end_keyword_loc: #{inspector.location(end_keyword_loc)}\n"
  inspector.to_str
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



5752
5753
5754
# File 'lib/prism/node.rb', line 5752

def type
  :for_node
end