Class: SyntaxTree::Words
Overview
Words represents a string literal array with interpolation.
%W[one two three]
Instance Attribute Summary collapse
-
#beginning ⇒ Object
readonly
- WordsBeg
-
the token that opens this array literal.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#elements ⇒ Object
readonly
- Array[ Word ]
-
the elements of this array.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
Instance Method Summary collapse
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(beginning:, elements:, location:, comments: []) ⇒ Words
constructor
A new instance of Words.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(beginning:, elements:, location:, comments: []) ⇒ Words
Returns a new instance of Words.
11654 11655 11656 11657 11658 11659 |
# File 'lib/syntax_tree/node.rb', line 11654 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
11643 11644 11645 |
# File 'lib/syntax_tree/node.rb', line 11643 def beginning @beginning end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
11652 11653 11654 |
# File 'lib/syntax_tree/node.rb', line 11652 def comments @comments end |
#elements ⇒ Object (readonly)
- Array[ Word ]
-
the elements of this array
11646 11647 11648 |
# File 'lib/syntax_tree/node.rb', line 11646 def elements @elements end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
11649 11650 11651 |
# File 'lib/syntax_tree/node.rb', line 11649 def location @location end |
Instance Method Details
#child_nodes ⇒ Object Also known as: deconstruct
11661 11662 11663 |
# File 'lib/syntax_tree/node.rb', line 11661 def child_nodes [] end |
#deconstruct_keys(keys) ⇒ Object
11667 11668 11669 11670 11671 11672 11673 11674 |
# File 'lib/syntax_tree/node.rb', line 11667 def deconstruct_keys(keys) { beginning: beginning, elements: elements, location: location, comments: comments } end |
#format(q) ⇒ Object
11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 |
# File 'lib/syntax_tree/node.rb', line 11676 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 |
#pretty_print(q) ⇒ Object
11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 |
# File 'lib/syntax_tree/node.rb', line 11695 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
11706 11707 11708 11709 11710 |
# File 'lib/syntax_tree/node.rb', line 11706 def to_json(*opts) { type: :words, elems: elements, loc: location, cmts: comments }.to_json( *opts ) end |