Class: Prism::InNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::InNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents the use of the ‘in` keyword in a case statement.
case a; in b then c end
^^^^^^^^^^^
Instance Attribute Summary collapse
-
#in_loc ⇒ Object
readonly
attr_reader in_loc: Location.
-
#pattern ⇒ Object
readonly
attr_reader pattern: Node.
-
#statements ⇒ Object
readonly
attr_reader statements: StatementsNode?.
-
#then_loc ⇒ Object
readonly
attr_reader then_loc: Location?.
Class Method Summary collapse
-
.type ⇒ Object
Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain.
Instance Method Summary collapse
-
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#copy(**params) ⇒ Object
def copy: (**params) -> InNode.
- #deconstruct_keys(keys) ⇒ Object
-
#in ⇒ Object
def in: () -> String.
-
#initialize(pattern, statements, in_loc, then_loc, location) ⇒ InNode
constructor
def initialize: (pattern: Node, statements: StatementsNode?, in_loc: Location, then_loc: Location?, location: Location) -> void.
-
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String.
-
#then ⇒ Object
def then: () -> String?.
-
#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(pattern, statements, in_loc, then_loc, location) ⇒ InNode
def initialize: (pattern: Node, statements: StatementsNode?, in_loc: Location, then_loc: Location?, location: Location) -> void
8144 8145 8146 8147 8148 8149 8150 |
# File 'lib/prism/node.rb', line 8144 def initialize(pattern, statements, in_loc, then_loc, location) @pattern = pattern @statements = statements @in_loc = in_loc @then_loc = then_loc @location = location end |
Instance Attribute Details
#in_loc ⇒ Object (readonly)
attr_reader in_loc: Location
8138 8139 8140 |
# File 'lib/prism/node.rb', line 8138 def in_loc @in_loc end |
#pattern ⇒ Object (readonly)
attr_reader pattern: Node
8132 8133 8134 |
# File 'lib/prism/node.rb', line 8132 def pattern @pattern end |
#statements ⇒ Object (readonly)
attr_reader statements: StatementsNode?
8135 8136 8137 |
# File 'lib/prism/node.rb', line 8135 def statements @statements end |
#then_loc ⇒ Object (readonly)
attr_reader then_loc: Location?
8141 8142 8143 |
# File 'lib/prism/node.rb', line 8141 def then_loc @then_loc end |
Class Method Details
.type ⇒ Object
Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.
def self.type: () -> Symbol
8244 8245 8246 |
# File 'lib/prism/node.rb', line 8244 def self.type :in_node end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
8153 8154 8155 |
# File 'lib/prism/node.rb', line 8153 def accept(visitor) visitor.visit_in_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
8158 8159 8160 |
# File 'lib/prism/node.rb', line 8158 def child_nodes [pattern, statements] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
8171 8172 8173 |
# File 'lib/prism/node.rb', line 8171 def comment_targets [pattern, *statements, in_loc, *then_loc] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
8163 8164 8165 8166 8167 8168 |
# File 'lib/prism/node.rb', line 8163 def compact_child_nodes compact = [] compact << pattern compact << statements if statements compact end |
#copy(**params) ⇒ Object
def copy: (**params) -> InNode
8176 8177 8178 8179 8180 8181 8182 8183 8184 |
# File 'lib/prism/node.rb', line 8176 def copy(**params) InNode.new( params.fetch(:pattern) { pattern }, params.fetch(:statements) { statements }, params.fetch(:in_loc) { in_loc }, params.fetch(:then_loc) { then_loc }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
8190 8191 8192 |
# File 'lib/prism/node.rb', line 8190 def deconstruct_keys(keys) { pattern: pattern, statements: statements, in_loc: in_loc, then_loc: then_loc, location: location } end |
#in ⇒ Object
def in: () -> String
8195 8196 8197 |
# File 'lib/prism/node.rb', line 8195 def in in_loc.slice end |
#inspect(inspector = NodeInspector.new) ⇒ Object
def inspect(inspector: NodeInspector) -> String
8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 |
# File 'lib/prism/node.rb', line 8205 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── pattern:\n" inspector << inspector.child_node(pattern, "│ ") if (statements = self.statements).nil? inspector << "├── statements: ∅\n" else inspector << "├── statements:\n" inspector << statements.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix) end inspector << "├── in_loc: #{inspector.location(in_loc)}\n" inspector << "└── then_loc: #{inspector.location(then_loc)}\n" inspector.to_str end |
#then ⇒ Object
def then: () -> String?
8200 8201 8202 |
# File 'lib/prism/node.rb', line 8200 def then then_loc&.slice 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
8234 8235 8236 |
# File 'lib/prism/node.rb', line 8234 def type :in_node end |