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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize a new ForNode node.



6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
# File 'lib/prism/node.rb', line 6275

def initialize(source, node_id, location, flags, index, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_loc, end_keyword_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @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
end

Instance Attribute Details

#collectionObject (readonly)

The collection to iterate over.

for i in a end
         ^


6336
6337
6338
# File 'lib/prism/node.rb', line 6336

def collection
  @collection
end

#indexObject (readonly)

The index expression for ‘for` loops.

for i in a end
    ^


6330
6331
6332
# File 'lib/prism/node.rb', line 6330

def index
  @index
end

#statementsObject (readonly)

Represents the body of statements to execute for each iteration of the loop.

for i in a
  foo(i)
  ^^^^^^
end


6344
6345
6346
# File 'lib/prism/node.rb', line 6344

def statements
  @statements
end

Class Method Details

.typeObject

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



6423
6424
6425
# File 'lib/prism/node.rb', line 6423

def self.type
  :for_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.



6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
# File 'lib/prism/node.rb', line 6429

def ===(other)
  other.is_a?(ForNode) &&
    (index === other.index) &&
    (collection === other.collection) &&
    (statements === other.statements) &&
    (for_keyword_loc.nil? == other.for_keyword_loc.nil?) &&
    (in_keyword_loc.nil? == other.in_keyword_loc.nil?) &&
    (do_keyword_loc.nil? == other.do_keyword_loc.nil?) &&
    (end_keyword_loc.nil? == other.end_keyword_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



6290
6291
6292
# File 'lib/prism/node.rb', line 6290

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

#child_nodesObject Also known as: deconstruct

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



6295
6296
6297
# File 'lib/prism/node.rb', line 6295

def child_nodes
  [index, collection, statements]
end

#comment_targetsObject

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



6309
6310
6311
# File 'lib/prism/node.rb', line 6309

def comment_targets
  [index, collection, *statements, for_keyword_loc, in_keyword_loc, *do_keyword_loc, end_keyword_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



6300
6301
6302
6303
6304
6305
6306
# File 'lib/prism/node.rb', line 6300

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << index
  compact << collection
  compact << statements if statements
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, index: self.index, collection: self.collection, statements: self.statements, for_keyword_loc: self.for_keyword_loc, in_keyword_loc: self.in_keyword_loc, do_keyword_loc: self.do_keyword_loc, end_keyword_loc: self.end_keyword_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?index: LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | BackReferenceReadNode | NumberedReferenceReadNode | MissingNode, ?collection: Prism::node, ?statements: StatementsNode?, ?for_keyword_loc: Location, ?in_keyword_loc: Location, ?do_keyword_loc: Location?, ?end_keyword_loc: Location) -> ForNode



6314
6315
6316
# File 'lib/prism/node.rb', line 6314

def copy(node_id: self.node_id, location: self.location, flags: self.flags, index: self.index, collection: self.collection, statements: self.statements, for_keyword_loc: self.for_keyword_loc, in_keyword_loc: self.in_keyword_loc, do_keyword_loc: self.do_keyword_loc, end_keyword_loc: self.end_keyword_loc)
  ForNode.new(source, node_id, location, flags, index, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_loc, end_keyword_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, index: LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | BackReferenceReadNode | NumberedReferenceReadNode | MissingNode, collection: Prism::node, statements: StatementsNode?, for_keyword_loc: Location, in_keyword_loc: Location, do_keyword_loc: Location?, end_keyword_loc: Location }



6322
6323
6324
# File 'lib/prism/node.rb', line 6322

def deconstruct_keys(keys)
  { node_id: node_id, location: 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 }
end

#do_keywordObject

def do_keyword: () -> String?



6403
6404
6405
# File 'lib/prism/node.rb', line 6403

def do_keyword
  do_keyword_loc&.slice
end

#do_keyword_locObject

The location of the ‘do` keyword, if present.

for i in a do end
           ^^


6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
# File 'lib/prism/node.rb', line 6370

def do_keyword_loc
  location = @do_keyword_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @do_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#end_keywordObject

def end_keyword: () -> String



6408
6409
6410
# File 'lib/prism/node.rb', line 6408

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

The location of the ‘end` keyword.

for i in a end
           ^^^


6386
6387
6388
6389
6390
# File 'lib/prism/node.rb', line 6386

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

#for_keywordObject

def for_keyword: () -> String



6393
6394
6395
# File 'lib/prism/node.rb', line 6393

def for_keyword
  for_keyword_loc.slice
end

#for_keyword_locObject

The location of the ‘for` keyword.

for i in a end
^^^


6350
6351
6352
6353
6354
# File 'lib/prism/node.rb', line 6350

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

#in_keywordObject

def in_keyword: () -> String



6398
6399
6400
# File 'lib/prism/node.rb', line 6398

def in_keyword
  in_keyword_loc.slice
end

#in_keyword_locObject

The location of the ‘in` keyword.

for i in a end
      ^^


6360
6361
6362
6363
6364
# File 'lib/prism/node.rb', line 6360

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

#inspectObject

def inspect -> String



6413
6414
6415
# File 'lib/prism/node.rb', line 6413

def inspect
  InspectVisitor.compose(self)
end

#typeObject

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



6418
6419
6420
# File 'lib/prism/node.rb', line 6418

def type
  :for_node
end