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.



4799
4800
4801
4802
4803
4804
4805
# File 'lib/syntax_tree.rb', line 4799

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



4788
4789
4790
# File 'lib/syntax_tree.rb', line 4788

def block_var
  @block_var
end

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed within this block



4791
4792
4793
# File 'lib/syntax_tree.rb', line 4791

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4797
4798
4799
# File 'lib/syntax_tree.rb', line 4797

def comments
  @comments
end

#keywordObject (readonly)

Kw

the do keyword that opens this block



4785
4786
4787
# File 'lib/syntax_tree.rb', line 4785

def keyword
  @keyword
end

#locationObject (readonly)

Location

the location of this node



4794
4795
4796
# File 'lib/syntax_tree.rb', line 4794

def location
  @location
end

Instance Method Details

#child_nodesObject



4807
4808
4809
# File 'lib/syntax_tree.rb', line 4807

def child_nodes
  [keyword, block_var, bodystmt]
end

#format(q) ⇒ Object



4811
4812
4813
# File 'lib/syntax_tree.rb', line 4811

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

#pretty_print(q) ⇒ Object



4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
# File 'lib/syntax_tree.rb', line 4815

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



4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
# File 'lib/syntax_tree.rb', line 4831

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