Class: SyntaxTree::Words

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

Overview

Words represents a string literal array with interpolation.

%W[one two three]

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(beginning:, elements:, location:, comments: []) ⇒ Words

Returns a new instance of Words.



9897
9898
9899
9900
9901
9902
# File 'lib/syntax_tree/node.rb', line 9897

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

Instance Attribute Details

#beginning ⇒ Object (readonly)

[WordsBeg] the token that opens this array literal



9889
9890
9891
# File 'lib/syntax_tree/node.rb', line 9889

def beginning
  @beginning
end

#comments ⇒ Object (readonly)

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



9895
9896
9897
# File 'lib/syntax_tree/node.rb', line 9895

def comments
  @comments
end

#elements ⇒ Object (readonly)

[Array[ Word ]] the elements of this array



9892
9893
9894
# File 'lib/syntax_tree/node.rb', line 9892

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



9904
9905
9906
# File 'lib/syntax_tree/node.rb', line 9904

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

#child_nodes ⇒ Object Also known as: deconstruct



9908
9909
9910
# File 'lib/syntax_tree/node.rb', line 9908

def child_nodes
  []
end

#deconstruct_keys(_keys) ⇒ Object



9914
9915
9916
9917
9918
9919
9920
9921
# File 'lib/syntax_tree/node.rb', line 9914

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

#format(q) ⇒ Object



9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
# File 'lib/syntax_tree/node.rb', line 9923

def format(q)
  opening, closing = "%W[", "]"

  if elements.any? { |element| element.match?(/[\[\]]/) }
    opening = beginning.value
    closing = Quotes.matching(opening[2])
  end

  q.group(0, opening, closing) do
    q.indent do
      q.breakable("")
      q.seplist(elements, -> { q.breakable }) do |element|
        q.format(element)
      end
    end
    q.breakable("")
  end
end