Class: Prism::InterpolatedStringNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::InterpolatedStringNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents a string literal that contains interpolation.
"foo #{bar} baz"
^^^^^^^^^^^^^^^^
Instance Attribute Summary collapse
-
#closing_loc ⇒ Object
readonly
attr_reader closing_loc: Location?.
-
#opening_loc ⇒ Object
readonly
attr_reader opening_loc: Location?.
-
#parts ⇒ Object
readonly
attr_reader parts: Array.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#closing ⇒ Object
def closing: () -> String?.
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#copy(**params) ⇒ Object
def copy: (**params) -> InterpolatedStringNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(opening_loc, parts, closing_loc, location) ⇒ InterpolatedStringNode
constructor
def initialize: (opening_loc: Location?, parts: Array, closing_loc: Location?, location: Location) -> void.
- #inspect(inspector = NodeInspector.new) ⇒ Object
-
#opening ⇒ Object
def opening: () -> String?.
- #set_newline_flag(newline_marked) ⇒ Object
-
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform.
Constructor Details
#initialize(opening_loc, parts, closing_loc, location) ⇒ InterpolatedStringNode
def initialize: (opening_loc: Location?, parts: Array, closing_loc: Location?, location: Location) -> void
8107 8108 8109 8110 8111 8112 |
# File 'lib/prism/node.rb', line 8107 def initialize(opening_loc, parts, closing_loc, location) @opening_loc = opening_loc @parts = parts @closing_loc = closing_loc @location = location end |
Instance Attribute Details
#closing_loc ⇒ Object (readonly)
attr_reader closing_loc: Location?
8104 8105 8106 |
# File 'lib/prism/node.rb', line 8104 def closing_loc @closing_loc end |
#opening_loc ⇒ Object (readonly)
attr_reader opening_loc: Location?
8098 8099 8100 |
# File 'lib/prism/node.rb', line 8098 def opening_loc @opening_loc end |
#parts ⇒ Object (readonly)
attr_reader parts: Array
8101 8102 8103 |
# File 'lib/prism/node.rb', line 8101 def parts @parts end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
8115 8116 8117 |
# File 'lib/prism/node.rb', line 8115 def accept(visitor) visitor.visit_interpolated_string_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
8125 8126 8127 |
# File 'lib/prism/node.rb', line 8125 def child_nodes [*parts] end |
#closing ⇒ Object
def closing: () -> String?
8163 8164 8165 |
# File 'lib/prism/node.rb', line 8163 def closing closing_loc&.slice end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
8135 8136 8137 |
# File 'lib/prism/node.rb', line 8135 def comment_targets [*opening_loc, *parts, *closing_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
8130 8131 8132 |
# File 'lib/prism/node.rb', line 8130 def compact_child_nodes [*parts] end |
#copy(**params) ⇒ Object
def copy: (**params) -> InterpolatedStringNode
8140 8141 8142 8143 8144 8145 8146 8147 |
# File 'lib/prism/node.rb', line 8140 def copy(**params) InterpolatedStringNode.new( params.fetch(:opening_loc) { opening_loc }, params.fetch(:parts) { parts }, params.fetch(:closing_loc) { closing_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
8153 8154 8155 |
# File 'lib/prism/node.rb', line 8153 def deconstruct_keys(keys) { opening_loc: opening_loc, parts: parts, closing_loc: closing_loc, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
8167 8168 8169 8170 8171 8172 8173 |
# File 'lib/prism/node.rb', line 8167 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n" inspector << "├── parts: #{inspector.list("#{inspector.prefix}│ ", parts)}" inspector << "└── closing_loc: #{inspector.location(closing_loc)}\n" inspector.to_str end |
#opening ⇒ Object
def opening: () -> String?
8158 8159 8160 |
# File 'lib/prism/node.rb', line 8158 def opening opening_loc&.slice end |
#set_newline_flag(newline_marked) ⇒ Object
8119 8120 8121 8122 |
# File 'lib/prism/node.rb', line 8119 def set_newline_flag(newline_marked) first = parts.first first.set_newline_flag(newline_marked) if first end |
#type ⇒ Object
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.
Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.
def type: () -> Symbol
8189 8190 8191 |
# File 'lib/prism/node.rb', line 8189 def type :interpolated_string_node end |