Class: Prism::InterpolatedStringNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
lib/prism/node_ext.rb,
lib/prism/parse_result/newlines.rb,
sig/generated/prism/node.rbs,
sig/generated/prism/node_ext.rbs,
sig/generated/prism/parse_result/newlines.rbs,
ext/prism/api_node.c

Overview

Represents a string literal that contains interpolation.

"foo #{bar} baz"
^^^^^^^^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, opening_loc, parts, closing_loc) ⇒ InterpolatedStringNode

Initialize a new InterpolatedStringNode node.

: (Source source, Integer node_id, Location location, Integer flags, Location? opening_loc, Array[StringNode | EmbeddedStatementsNode | EmbeddedVariableNode | InterpolatedStringNode] parts, Location? closing_loc) -> void

Parameters:



19879
19880
19881
19882
19883
19884
19885
19886
19887
# File 'lib/prism/node.rb', line 19879

def initialize(source, node_id, location, flags, opening_loc, parts, closing_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @opening_loc = opening_loc
  @parts = parts
  @closing_loc = closing_loc
end

Class Method Details

.type ⇒ :interpolated_string_node

See Node.type.

: () -> :interpolated_string_node

Returns:

  • (:interpolated_string_node)


19970
19971
19972
# File 'lib/prism/node.rb', line 19970

def self.type
  :interpolated_string_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


20086
20087
20088
20089
20090
20091
20092
20093
# File 'lib/prism/node.rb', line 20086

def ===(other) # :nodoc:
  other.is_a?(InterpolatedStringNode) &&
    (flags === other.flags) &&
    (opening_loc.nil? == other.opening_loc.nil?) &&
    (parts.length == other.parts.length) &&
    parts.zip(other.parts).all? { |left, right| left === right } &&
    (closing_loc.nil? == other.closing_loc.nil?)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


19903
19904
19905
# File 'lib/prism/node.rb', line 19903

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

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


19910
19911
19912
# File 'lib/prism/node.rb', line 19910

def child_nodes
  [*parts]
end

#closing ⇒ String?

:call-seq:

closing -> String | nil

Slice the location of closing_loc from the source.

: () -> String?

Returns:

  • (String, nil)


20079
20080
20081
# File 'lib/prism/node.rb', line 20079

def closing
  closing_loc&.slice
end

#closing_loc ⇒ Location?

:category: Locations :call-seq:

closing_loc -> Location | nil

Returns the Location represented by closing_loc.

: () -> Location?

Returns:



20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
# File 'lib/prism/node.rb', line 20041

def closing_loc
  location = @closing_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @closing_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


19927
19928
19929
# File 'lib/prism/node.rb', line 19927

def compact_child_nodes
  [*parts]
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, opening_loc: self.opening_loc, parts: self.parts, closing_loc: self.closing_loc) ⇒ InterpolatedStringNode

:call-seq:

copy(**fields) -> InterpolatedStringNode

Creates a copy of self with the given fields, using self as the template.

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?opening_loc: Location?, ?parts: Array[StringNode | EmbeddedStatementsNode | EmbeddedVariableNode | InterpolatedStringNode], ?closing_loc: Location?) -> InterpolatedStringNode

Parameters:

Returns:



19937
19938
19939
# File 'lib/prism/node.rb', line 19937

def copy(node_id: self.node_id, location: self.location, flags: self.flags, opening_loc: self.opening_loc, parts: self.parts, closing_loc: self.closing_loc)
  InterpolatedStringNode.new(source, node_id, location, flags, opening_loc, parts, closing_loc)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
# File 'lib/prism/node.rb', line 19944

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location opening_loc parts closing_loc opening closing]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :opening_loc             then deconstructed[:opening_loc] = self.opening_loc
    when :parts                   then deconstructed[:parts] = self.parts
    when :closing_loc             then deconstructed[:closing_loc] = self.closing_loc
    when :opening                 then deconstructed[:opening] = self.opening
    when :closing                 then deconstructed[:closing] = self.closing
    end
  end
end

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


19918
19919
19920
19921
19922
# File 'lib/prism/node.rb', line 19918

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  parts.each { |node| yield node }
end

#frozen? ⇒ Boolean

:category: Flags frozen by virtue of a frozen_string_literal: true comment or --enable-frozen-string-literal; only for adjacent string literals like 'a' 'b'

: () -> bool

Returns:

  • (Boolean)


19985
19986
19987
# File 'lib/prism/node.rb', line 19985

def frozen?
  flags.anybits?(InterpolatedStringNodeFlags::FROZEN)
end

#heredoc? ⇒ Boolean?

Returns true if this node was represented as a heredoc in the source code.

: () -> bool?

Returns:

  • (Boolean, nil)


97
98
99
# File 'lib/prism/node_ext.rb', line 97

def heredoc?
  HeredocQuery.heredoc?(opening)
end

#inspect ⇒ String

: () -> String

Returns:

  • (String)


19975
19976
19977
# File 'lib/prism/node.rb', line 19975

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#mutable? ⇒ Boolean

:category: Flags mutable by virtue of a frozen_string_literal: false comment or --disable-frozen-string-literal; only for adjacent string literals like 'a' 'b'

: () -> bool

Returns:

  • (Boolean)


19993
19994
19995
# File 'lib/prism/node.rb', line 19993

def mutable?
  flags.anybits?(InterpolatedStringNodeFlags::MUTABLE)
end

#newline_flag!(lines) ⇒ void

This method returns an undefined value.

: (Array lines) -> void

Parameters:

  • lines (Array[bool])


398
399
400
401
402
403
404
405
# File 'lib/prism/parse_result/newlines.rb', line 398

def newline_flag!(lines) # :nodoc:
  first = parts.first
  if first && !static_literal?
    first.newline_flag!(lines)
  else
    super
  end
end

#opening ⇒ String?

:call-seq:

opening -> String | nil

Slice the location of opening_loc from the source.

: () -> String?

Returns:

  • (String, nil)


20069
20070
20071
# File 'lib/prism/node.rb', line 20069

def opening
  opening_loc&.slice
end

#opening_loc ⇒ Location?

:category: Locations :call-seq:

opening_loc -> Location | nil

Returns the Location represented by opening_loc.

: () -> Location?

Returns:



20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
# File 'lib/prism/node.rb', line 20004

def opening_loc
  location = @opening_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @opening_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#parts ⇒ Array[StringNode | EmbeddedStatementsNode | EmbeddedVariableNode | InterpolatedStringNode]

:call-seq:

parts -> Array[StringNode | EmbeddedStatementsNode | EmbeddedVariableNode | InterpolatedStringNode]

Returns the parts attribute.

: () -> Array[StringNode | EmbeddedStatementsNode | EmbeddedVariableNode | InterpolatedStringNode]



20030
20031
20032
# File 'lib/prism/node.rb', line 20030

def parts
  @parts
end

#save_closing_loc(repository) ⇒ Relocation::Entry?

:category: Repository Save the closing_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry?

Parameters:

Returns:



20058
20059
20060
# File 'lib/prism/node.rb', line 20058

def save_closing_loc(repository)
  repository.enter(node_id, :closing_loc) unless @closing_loc.nil?
end

#save_opening_loc(repository) ⇒ Relocation::Entry?

:category: Repository Save the opening_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry?

Parameters:

Returns:



20021
20022
20023
# File 'lib/prism/node.rb', line 20021

def save_opening_loc(repository)
  repository.enter(node_id, :opening_loc) unless @opening_loc.nil?
end

#type ⇒ :interpolated_string_node

See Node#type.

: () -> :interpolated_string_node

Returns:

  • (:interpolated_string_node)


19963
19964
19965
# File 'lib/prism/node.rb', line 19963

def type
  :interpolated_string_node
end