Class: SyntaxTree::Words

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

Overview

Words represents a string literal array with interpolation.

%W[one two three]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements:, location:, comments: []) ⇒ Words

Returns a new instance of Words.



12809
12810
12811
12812
12813
# File 'lib/syntax_tree.rb', line 12809

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



12807
12808
12809
# File 'lib/syntax_tree.rb', line 12807

def comments
  @comments
end

#elementsObject (readonly)

Array[ Word ]

the elements of this array



12801
12802
12803
# File 'lib/syntax_tree.rb', line 12801

def elements
  @elements
end

#locationObject (readonly)

Location

the location of this node



12804
12805
12806
# File 'lib/syntax_tree.rb', line 12804

def location
  @location
end

Instance Method Details

#child_nodesObject



12815
12816
12817
# File 'lib/syntax_tree.rb', line 12815

def child_nodes
  []
end

#format(q) ⇒ Object



12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
# File 'lib/syntax_tree.rb', line 12819

def format(q)
  q.group(0, "%W[", "]") do
    q.indent do
      q.breakable("")
      q.seplist(elements, -> { q.breakable }) do |element|
        q.format(element)
      end
    end
    q.breakable("")
  end
end

#pretty_print(q) ⇒ Object



12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
# File 'lib/syntax_tree.rb', line 12831

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

    q.breakable
    q.group(2, "(", ")") { q.seplist(elements) { |element| q.pp(element) } }

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

#to_json(*opts) ⇒ Object



12842
12843
12844
12845
12846
# File 'lib/syntax_tree.rb', line 12842

def to_json(*opts)
  { type: :words, elems: elements, loc: location, cmts: comments }.to_json(
    *opts
  )
end