Class: SyntaxTree::Begin

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

Overview

Begin represents a begin..end chain.

begin
  value
end

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(bodystmt:, location:, comments: []) ⇒ Begin

Returns a new instance of Begin.



1532
1533
1534
1535
1536
# File 'lib/syntax_tree/node.rb', line 1532

def initialize(bodystmt:, location:, comments: [])
  @bodystmt = bodystmt
  @location = location
  @comments = comments
end

Instance Attribute Details

#bodystmt ⇒ Object (readonly)

[BodyStmt] the bodystmt that contains the contents of this begin block



1527
1528
1529
# File 'lib/syntax_tree/node.rb', line 1527

def bodystmt
  @bodystmt
end

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



1530
1531
1532
# File 'lib/syntax_tree/node.rb', line 1530

def comments
  @comments
end

Instance Method Details

#accept(visitor) ⇒ Object



1538
1539
1540
# File 'lib/syntax_tree/node.rb', line 1538

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

#child_nodes ⇒ Object Also known as: deconstruct



1542
1543
1544
# File 'lib/syntax_tree/node.rb', line 1542

def child_nodes
  [bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



1548
1549
1550
# File 'lib/syntax_tree/node.rb', line 1548

def deconstruct_keys(_keys)
  { bodystmt: bodystmt, location: location, comments: comments }
end

#format(q) ⇒ Object



1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
# File 'lib/syntax_tree/node.rb', line 1552

def format(q)
  q.text("begin")

  unless bodystmt.empty?
    q.indent do
      q.breakable(force: true) unless bodystmt.statements.empty?
      q.format(bodystmt)
    end
  end

  q.breakable(force: true)
  q.text("end")
end