Class: Prism::BlockNode

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

Overview

Represents a block of ruby code.

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

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize a new BlockNode node.

: (Source source, Integer node_id, Location location, Integer flags, Array locals, (BlockParametersNode | NumberedParametersNode | ItParametersNode)? parameters, (StatementsNode | BeginNode)? body, Location opening_loc, Location closing_loc) -> void

Parameters:



3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
# File 'lib/prism/node.rb', line 3198

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

Class Method Details

.type ⇒ :block_node

See Node.type.

: () -> :block_node

Returns:

  • (:block_node)


3297
3298
3299
# File 'lib/prism/node.rb', line 3297

def self.type
  :block_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


3446
3447
3448
3449
3450
3451
3452
3453
3454
# File 'lib/prism/node.rb', line 3446

def ===(other) # :nodoc:
  other.is_a?(BlockNode) &&
    (locals.length == other.locals.length) &&
    locals.zip(other.locals).all? { |left, right| left === right } &&
    (parameters === other.parameters) &&
    (body === other.body) &&
    (opening_loc.nil? == other.opening_loc.nil?) &&
    (closing_loc.nil? == other.closing_loc.nil?)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


3224
3225
3226
# File 'lib/prism/node.rb', line 3224

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

#body ⇒ StatementsNode, ...

:call-seq:

body -> StatementsNode | BeginNode | nil

The body of the block.

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

Explore code sample

: () -> (StatementsNode | BeginNode)?

Returns:



3359
3360
3361
# File 'lib/prism/node.rb', line 3359

def body
  @body
end

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


3231
3232
3233
# File 'lib/prism/node.rb', line 3231

def child_nodes
  [parameters, body]
end

#closing ⇒ String

:call-seq:

closing -> String

Slice the location of closing_loc from the source.

: () -> String

Returns:

  • (String)


3439
3440
3441
# File 'lib/prism/node.rb', line 3439

def closing
  closing_loc.slice
end

#closing_loc ⇒ Location

:category: Locations :call-seq:

closing_loc -> Location

Represents the Location of the closing } or end.

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

Explore code sample

: () -> Location

Returns:



3406
3407
3408
3409
3410
# File 'lib/prism/node.rb', line 3406

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

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


3249
3250
3251
3252
3253
3254
# File 'lib/prism/node.rb', line 3249

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  if (parameters = self.parameters); compact << parameters; end
  if (body = self.body); compact << body; end
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, locals: self.locals, parameters: self.parameters, body: self.body, opening_loc: self.opening_loc, closing_loc: self.closing_loc) ⇒ BlockNode

:call-seq:

copy(**fields) -> BlockNode

Creates a copy of self with the given fields, using self as the template.

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?locals: Array, ?parameters: (BlockParametersNode | NumberedParametersNode | ItParametersNode)?, ?body: (StatementsNode | BeginNode)?, ?opening_loc: Location, ?closing_loc: Location) -> BlockNode

Parameters:

Returns:



3262
3263
3264
# File 'lib/prism/node.rb', line 3262

def copy(node_id: self.node_id, location: self.location, flags: self.flags, locals: self.locals, parameters: self.parameters, body: self.body, opening_loc: self.opening_loc, closing_loc: self.closing_loc)
  BlockNode.new(source, node_id, location, flags, locals, parameters, body, opening_loc, closing_loc)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
# File 'lib/prism/node.rb', line 3269

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location locals parameters body opening_loc closing_loc opening closing]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :locals                  then deconstructed[:locals] = self.locals
    when :parameters              then deconstructed[:parameters] = self.parameters
    when :body                    then deconstructed[:body] = self.body
    when :opening_loc             then deconstructed[:opening_loc] = self.opening_loc
    when :closing_loc             then deconstructed[:closing_loc] = self.closing_loc
    when :opening                 then deconstructed[:opening] = self.opening
    when :closing                 then deconstructed[:closing] = self.closing
    end
  end
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


3239
3240
3241
3242
3243
3244
# File 'lib/prism/node.rb', line 3239

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  if (parameters = self.parameters); yield parameters; end
  if (body = self.body); yield body; end
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


3302
3303
3304
# File 'lib/prism/node.rb', line 3302

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#locals ⇒ Array[Symbol]

:call-seq:

locals -> Array[Symbol]

The local variables declared in the block.

[1, 2, 3].each { |i| puts x } # locals: [:i]
                  ^

Explore code sample

: () -> Array

Returns:

  • (Array[Symbol])


3321
3322
3323
# File 'lib/prism/node.rb', line 3321

def locals
  @locals
end

#opening ⇒ String

:call-seq:

opening -> String

Slice the location of opening_loc from the source.

: () -> String

Returns:

  • (String)


3429
3430
3431
# File 'lib/prism/node.rb', line 3429

def opening
  opening_loc.slice
end

#opening_loc ⇒ Location

:category: Locations :call-seq:

opening_loc -> Location

Represents the Location of the opening { or do.

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

Explore code sample

: () -> Location

Returns:



3377
3378
3379
3380
3381
# File 'lib/prism/node.rb', line 3377

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

#parameters ⇒ BlockParametersNode, ...

:call-seq:

parameters -> BlockParametersNode | NumberedParametersNode | ItParametersNode | nil

The parameters of the block.

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

Explore code sample

: () -> (BlockParametersNode | NumberedParametersNode | ItParametersNode)?



3342
3343
3344
# File 'lib/prism/node.rb', line 3342

def parameters
  @parameters
end

#save_closing_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the closing_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



3417
3418
3419
# File 'lib/prism/node.rb', line 3417

def save_closing_loc(repository)
  repository.enter(node_id, :closing_loc)
end

#save_opening_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the opening_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



3388
3389
3390
# File 'lib/prism/node.rb', line 3388

def save_opening_loc(repository)
  repository.enter(node_id, :opening_loc)
end

#type ⇒ :block_node

See Node#type.

: () -> :block_node

Returns:

  • (:block_node)


3290
3291
3292
# File 'lib/prism/node.rb', line 3290

def type
  :block_node
end