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

#pretty_print, #to_json

Constructor Details

#initialize(bodystmt:, location:, comments: []) ⇒ Begin

Returns a new instance of Begin.



1398
1399
1400
1401
1402
# File 'lib/syntax_tree/node.rb', line 1398

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

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the bodystmt that contains the contents of this begin block



1393
1394
1395
# File 'lib/syntax_tree/node.rb', line 1393

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1396
1397
1398
# File 'lib/syntax_tree/node.rb', line 1396

def comments
  @comments
end

Instance Method Details

#accept(visitor) ⇒ Object



1404
1405
1406
# File 'lib/syntax_tree/node.rb', line 1404

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

#child_nodesObject Also known as: deconstruct



1408
1409
1410
# File 'lib/syntax_tree/node.rb', line 1408

def child_nodes
  [bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



1414
1415
1416
# File 'lib/syntax_tree/node.rb', line 1414

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

#format(q) ⇒ Object



1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
# File 'lib/syntax_tree/node.rb', line 1418

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