Class: Prism::BlockNode

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

Overview

Represents a block of ruby code.

[1, 2, 3].each { |i| puts x }

^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locals, parameters, body, opening_loc, closing_loc, location) ⇒ BlockNode

def initialize: (locals: Array, parameters: BlockParametersNode?, body: Node?, opening_loc: Location, closing_loc: Location, location: Location) -> void



1330
1331
1332
1333
1334
1335
1336
1337
# File 'lib/prism/node.rb', line 1330

def initialize(locals, parameters, body, opening_loc, closing_loc, location)
  @locals = locals
  @parameters = parameters
  @body = body
  @opening_loc = opening_loc
  @closing_loc = closing_loc
  @location = location
end

Instance Attribute Details

#bodyObject (readonly)

attr_reader body: Node?



1321
1322
1323
# File 'lib/prism/node.rb', line 1321

def body
  @body
end

#closing_locObject (readonly)

attr_reader closing_loc: Location



1327
1328
1329
# File 'lib/prism/node.rb', line 1327

def closing_loc
  @closing_loc
end

#localsObject (readonly)

attr_reader locals: Array



1315
1316
1317
# File 'lib/prism/node.rb', line 1315

def locals
  @locals
end

#opening_locObject (readonly)

attr_reader opening_loc: Location



1324
1325
1326
# File 'lib/prism/node.rb', line 1324

def opening_loc
  @opening_loc
end

#parametersObject (readonly)

attr_reader parameters: BlockParametersNode?



1318
1319
1320
# File 'lib/prism/node.rb', line 1318

def parameters
  @parameters
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



1340
1341
1342
# File 'lib/prism/node.rb', line 1340

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

#child_nodesObject Also known as: deconstruct

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



1345
1346
1347
# File 'lib/prism/node.rb', line 1345

def child_nodes
  [parameters, body]
end

#closingObject

def closing: () -> String



1388
1389
1390
# File 'lib/prism/node.rb', line 1388

def closing
  closing_loc.slice
end

#comment_targetsObject

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



1358
1359
1360
# File 'lib/prism/node.rb', line 1358

def comment_targets
  [*parameters, *body, opening_loc, closing_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



1350
1351
1352
1353
1354
1355
# File 'lib/prism/node.rb', line 1350

def compact_child_nodes
  compact = []
  compact << parameters if parameters
  compact << body if body
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> BlockNode



1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
# File 'lib/prism/node.rb', line 1363

def copy(**params)
  BlockNode.new(
    params.fetch(:locals) { locals },
    params.fetch(:parameters) { parameters },
    params.fetch(:body) { body },
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



1378
1379
1380
# File 'lib/prism/node.rb', line 1378

def deconstruct_keys(keys)
  { locals: locals, parameters: parameters, body: body, opening_loc: opening_loc, closing_loc: closing_loc, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
# File 'lib/prism/node.rb', line 1392

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

#openingObject

def opening: () -> String



1383
1384
1385
# File 'lib/prism/node.rb', line 1383

def opening
  opening_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



1426
1427
1428
# File 'lib/prism/node.rb', line 1426

def type
  :block_node
end