Class: SyntaxTree::DoBlock

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

Overview

DoBlock represents passing a block to a method call using the do and end keywords.

method do |value|
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword:, block_var:, bodystmt:, location:, comments: []) ⇒ DoBlock

Returns a new instance of DoBlock.



4836
4837
4838
4839
4840
4841
4842
# File 'lib/syntax_tree.rb', line 4836

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

Instance Attribute Details

#block_varObject (readonly)

nil | BlockVar

the optional variable declaration within this block



4825
4826
4827
# File 'lib/syntax_tree.rb', line 4825

def block_var
  @block_var
end

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed within this block



4828
4829
4830
# File 'lib/syntax_tree.rb', line 4828

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4834
4835
4836
# File 'lib/syntax_tree.rb', line 4834

def comments
  @comments
end

#keywordObject (readonly)

Kw

the do keyword that opens this block



4822
4823
4824
# File 'lib/syntax_tree.rb', line 4822

def keyword
  @keyword
end

#locationObject (readonly)

Location

the location of this node



4831
4832
4833
# File 'lib/syntax_tree.rb', line 4831

def location
  @location
end

Instance Method Details

#child_nodesObject



4844
4845
4846
# File 'lib/syntax_tree.rb', line 4844

def child_nodes
  [keyword, block_var, bodystmt]
end

#format(q) ⇒ Object



4848
4849
4850
# File 'lib/syntax_tree.rb', line 4848

def format(q)
  BlockFormatter.new(self, keyword, "end", bodystmt).format(q)
end

#pretty_print(q) ⇒ Object



4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
# File 'lib/syntax_tree.rb', line 4852

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("do_block")

    if block_var
      q.breakable
      q.pp(block_var)
    end

    q.breakable
    q.pp(bodystmt)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
# File 'lib/syntax_tree.rb', line 4868

def to_json(*opts)
  {
    type: :do_block,
    keyword: keyword,
    block_var: block_var,
    bodystmt: bodystmt,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end