Class: SyntaxTree::BraceBlock

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

BraceBlock represents passing a block to a method call using the { } operators.

method { |variable| variable + 1 }

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(lbrace:, block_var:, statements:, location:, comments: []) ⇒ BraceBlock

Returns a new instance of BraceBlock.



2063
2064
2065
2066
2067
2068
2069
# File 'lib/syntax_tree/node.rb', line 2063

def initialize(lbrace:, block_var:, statements:, location:, comments: [])
  @lbrace = lbrace
  @block_var = block_var
  @statements = statements
  @location = location
  @comments = comments
end

Instance Attribute Details

#block_varObject (readonly)

nil | BlockVar

the optional set of parameters to the block



2055
2056
2057
# File 'lib/syntax_tree/node.rb', line 2055

def block_var
  @block_var
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2061
2062
2063
# File 'lib/syntax_tree/node.rb', line 2061

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that opens this block



2052
2053
2054
# File 'lib/syntax_tree/node.rb', line 2052

def lbrace
  @lbrace
end

#statementsObject (readonly)

Statements

the list of expressions to evaluate within the block



2058
2059
2060
# File 'lib/syntax_tree/node.rb', line 2058

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



2071
2072
2073
# File 'lib/syntax_tree/node.rb', line 2071

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

#child_nodesObject Also known as: deconstruct



2075
2076
2077
# File 'lib/syntax_tree/node.rb', line 2075

def child_nodes
  [lbrace, block_var, statements]
end

#deconstruct_keys(_keys) ⇒ Object



2081
2082
2083
2084
2085
2086
2087
2088
2089
# File 'lib/syntax_tree/node.rb', line 2081

def deconstruct_keys(_keys)
  {
    lbrace: lbrace,
    block_var: block_var,
    statements: statements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



2091
2092
2093
# File 'lib/syntax_tree/node.rb', line 2091

def format(q)
  BlockFormatter.new(self, lbrace, "}", statements).format(q)
end