class SyntaxTree::BlockNode

Block represents passing a block to a method call using the do and end keywords or the +{+ and +}+ operators.

method do |value|
end

method { |value| }

Attributes

block_var[R]
nil | BlockVar

the optional variable declaration within this block

bodystmt[R]
BodyStmt | Statements

the expressions to be executed within this block

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

opening[R]
LBrace | Kw

the left brace or the do keyword that opens this block

Public Class Methods

new(opening:, block_var:, bodystmt:, location:) click to toggle source
# File lib/syntax_tree/node.rb, line 4347
def initialize(opening:, block_var:, bodystmt:, location:)
  @opening = opening
  @block_var = block_var
  @bodystmt = bodystmt
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 4420
def ===(other)
  other.is_a?(BlockNode) && opening === other.opening &&
    block_var === other.block_var && bodystmt === other.bodystmt
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 4355
def accept(visitor)
  visitor.visit_block(self)
end
arity() click to toggle source
# File lib/syntax_tree/node.rb, line 4429
def arity
  case block_var
  when BlockVar
    block_var.params.arity
  else
    0..0
  end
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 4359
def child_nodes
  [opening, block_var, bodystmt]
end
Also aliased as: deconstruct
copy(opening: nil, block_var: nil, bodystmt: nil, location: nil) click to toggle source
# File lib/syntax_tree/node.rb, line 4363
def copy(opening: nil, block_var: nil, bodystmt: nil, location: nil)
  node =
    BlockNode.new(
      opening: opening || self.opening,
      block_var: block_var || self.block_var,
      bodystmt: bodystmt || self.bodystmt,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end
deconstruct()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 4378
def deconstruct_keys(_keys)
  {
    opening: opening,
    block_var: block_var,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 4388
def format(q)
  # If this is nested anywhere inside of a Command or CommandCall node, then
  # we can't change which operators we're using for the bounds of the block.
  break_opening, break_closing, flat_opening, flat_closing =
    if unchangeable_bounds?(q)
      block_close = keywords? ? "end" : "}"
      [opening.value, block_close, opening.value, block_close]
    elsif forced_do_end_bounds?(q)
      %w[do end do end]
    elsif forced_brace_bounds?(q)
      %w[{ } { }]
    else
      %w[do end { }]
    end

  # If the receiver of this block a Command or CommandCall node, then there
  # are no parentheses around the arguments to that command, so we need to
  # break the block.
  case q.parent
  when nil, Command, CommandCall
    q.break_parent
    format_break(q, break_opening, break_closing)
    return
  end

  q.group do
    q
      .if_break { format_break(q, break_opening, break_closing) }
      .if_flat { format_flat(q, flat_opening, flat_closing) }
  end
end
keywords?() click to toggle source
# File lib/syntax_tree/node.rb, line 4425
def keywords?
  opening.is_a?(Kw)
end