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:, comments: []) ⇒ Word

Returns a new instance of Word.



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

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9902
9903
9904
# File 'lib/syntax_tree/node.rb', line 9902

def comments
  @comments
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

word



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

def parts
  @parts
end

Instance Method Details

#accept(visitor) ⇒ Object



9914
9915
9916
# File 'lib/syntax_tree/node.rb', line 9914

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

#child_nodesObject Also known as: deconstruct



9918
9919
9920
# File 'lib/syntax_tree/node.rb', line 9918

def child_nodes
  parts
end

#deconstruct_keys(_keys) ⇒ Object



9924
9925
9926
# File 'lib/syntax_tree/node.rb', line 9924

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

#format(q) ⇒ Object



9928
9929
9930
# File 'lib/syntax_tree/node.rb', line 9928

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

#match?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


9910
9911
9912
# File 'lib/syntax_tree/node.rb', line 9910

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