Class: SyntaxTree::Word

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

Overview

Word represents an element within a special array literal that accepts interpolation.

%W[a#{b}c xyz]

In the example above, there would be two Word nodes within a parent Words node.

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(parts:, location:) ⇒ Word

Returns a new instance of Word.



11834
11835
11836
11837
11838
# File 'lib/syntax_tree/node.rb', line 11834

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11832
11833
11834
# File 'lib/syntax_tree/node.rb', line 11832

def comments
  @comments
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

word



11829
11830
11831
# File 'lib/syntax_tree/node.rb', line 11829

def parts
  @parts
end

Instance Method Details

#===(other) ⇒ Object



11873
11874
11875
# File 'lib/syntax_tree/node.rb', line 11873

def ===(other)
  other.is_a?(Word) && ArrayMatch.call(parts, other.parts)
end

#accept(visitor) ⇒ Object



11844
11845
11846
# File 'lib/syntax_tree/node.rb', line 11844

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

#child_nodesObject Also known as: deconstruct



11848
11849
11850
# File 'lib/syntax_tree/node.rb', line 11848

def child_nodes
  parts
end

#copy(parts: nil, location: nil) ⇒ Object



11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
# File 'lib/syntax_tree/node.rb', line 11852

def copy(parts: nil, location: nil)
  node =
    Word.new(
      parts: parts || self.parts,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



11865
11866
11867
# File 'lib/syntax_tree/node.rb', line 11865

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

#format(q) ⇒ Object



11869
11870
11871
# File 'lib/syntax_tree/node.rb', line 11869

def format(q)
  q.format_each(parts)
end

#match?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


11840
11841
11842
# File 'lib/syntax_tree/node.rb', line 11840

def match?(pattern)
  parts.any? { |part| part.is_a?(TStringContent) && part.match?(pattern) }
end