Class: Prism::RescueNode
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Node
|
|
Instance Chain:
self,
Node
|
|
Inherits: |
Prism::Node
|
Defined in: | lib/prism/node.rb |
Overview
Represents a rescue statement.
begin
rescue Foo, *splat, Bar => ex
foo
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
end
Foo, *splat, Bar
are in the #exceptions field. ex
is in the exception
field.
Class Method Summary
- .new(source, keyword_loc, exceptions, operator_loc, reference, statements, consequent, location) ⇒ RescueNode constructor
-
.type
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.
Node
- Inherited
Instance Attribute Summary
-
#consequent
readonly
attr_reader consequent: RescueNode?
-
#exceptions
readonly
attr_reader exceptions: Array.
-
#reference
readonly
attr_reader reference:
Prism.node?
-
#statements
readonly
attr_reader statements: StatementsNode?
Node
- Inherited
Instance Method Summary
-
#===(other)
Implements case-equality for the node.
-
#accept(visitor)
def accept: (Visitor visitor) -> void.
-
#child_nodes
(also: #deconstruct)
def child_nodes: () -> Array[nil | Node].
-
#comment_targets
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes
def compact_child_nodes: () -> Array.
- #copy(keyword_loc: self.keyword_loc, exceptions: self.exceptions, operator_loc: self.operator_loc, reference: self.reference, statements: self.statements, consequent: self.consequent, location: self.location)
-
#deconstruct
Alias for #child_nodes.
- #deconstruct_keys(keys)
-
#inspect
def inspect -> String.
-
#keyword
def keyword: () -> String.
-
#keyword_loc
attr_reader keyword_loc:
Location
. -
#operator
def operator: () -> String?
-
#operator_loc
attr_reader operator_loc: Location?
-
#type
Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform.
Node
- Inherited
#accept | Accepts a visitor and calls back into the specialized visit function. |
#child_nodes | Returns an array of child nodes, including |
#comment_targets | Returns an array of child nodes and locations that could potentially have comments attached to them. |
#compact_child_nodes | Returns an array of child nodes, excluding any |
#deconstruct | Alias for Node#child_nodes. |
#end_offset | The end offset of the node in the source. |
#inspect | Returns a string representation of the node. |
#location | A Location instance that represents the location of this node in the source. |
#pretty_print | Similar to inspect, but respects the current level of indentation given by the pretty print object. |
#script_lines | Alias for Node#source_lines. |
#slice | Slice the location of the node from the source. |
#slice_lines | Slice the location of the node from the source, starting at the beginning of the line that the location starts on, ending at the end of the line that the location ends on. |
#source_lines | Returns all of the lines of the source code associated with this node. |
#start_offset | The start offset of the node in the source. |
#to_dot | Convert this node into a graphviz dot graph string. |
#tunnel | Returns a list of nodes that are descendants of this node that contain the given line and column. |
#type | Returns a symbol symbolizing the type of node that this represents. |
#deprecated, #newline! |
Constructor Details
.new(source, keyword_loc, exceptions, operator_loc, reference, statements, consequent, location) ⇒ RescueNode
# File 'lib/prism/node.rb', line 16249
def initialize(source, keyword_loc, exceptions, operator_loc, reference, statements, consequent, location) @source = source @location = location @keyword_loc = keyword_loc @exceptions = exceptions @operator_loc = operator_loc @reference = reference @statements = statements @consequent = consequent end
Class Method Details
.type
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
# File 'lib/prism/node.rb', line 16369
def self.type :rescue_node end
Instance Attribute Details
#consequent (readonly)
attr_reader consequent: RescueNode?
# File 'lib/prism/node.rb', line 16328
attr_reader :consequent
#exceptions (readonly)
attr_reader exceptions: Array
# File 'lib/prism/node.rb', line 16306
attr_reader :exceptions
#reference (readonly)
attr_reader reference: Prism.node?
# File 'lib/prism/node.rb', line 16322
attr_reader :reference
#statements (readonly)
attr_reader statements: StatementsNode?
# File 'lib/prism/node.rb', line 16325
attr_reader :statements
Instance Method Details
#===(other)
Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.
# File 'lib/prism/node.rb', line 16375
def ===(other) other.is_a?(RescueNode) && (keyword_loc.nil? == other.keyword_loc.nil?) && (exceptions.length == other.exceptions.length) && exceptions.zip(other.exceptions).all? { |left, right| left === right } && (operator_loc.nil? == other.operator_loc.nil?) && (reference === other.reference) && (statements === other.statements) && (consequent === other.consequent) end
#accept(visitor)
def accept: (Visitor visitor) -> void
# File 'lib/prism/node.rb', line 16261
def accept(visitor) visitor.visit_rescue_node(self) end
#child_nodes Also known as: #deconstruct
def child_nodes: () -> Array[nil | Node]
# File 'lib/prism/node.rb', line 16266
def child_nodes [*exceptions, reference, statements, consequent] end
#comment_targets
def comment_targets: () -> Array[Node | Location]
# File 'lib/prism/node.rb', line 16281
def comment_targets [keyword_loc, *exceptions, *operator_loc, *reference, *statements, *consequent] #: Array[Prism::node | Location] end
#compact_child_nodes
def compact_child_nodes: () -> Array
# File 'lib/prism/node.rb', line 16271
def compact_child_nodes compact = [] #: Array[Prism::node] compact.concat(exceptions) compact << reference if reference compact << statements if statements compact << consequent if consequent compact end
#copy(keyword_loc: self.keyword_loc, exceptions: self.exceptions, operator_loc: self.operator_loc, reference: self.reference, statements: self.statements, consequent: self.consequent, location: self.location)
# File 'lib/prism/node.rb', line 16286
def copy(keyword_loc: self.keyword_loc, exceptions: self.exceptions, operator_loc: self.operator_loc, reference: self.reference, statements: self.statements, consequent: self.consequent, location: self.location) RescueNode.new(source, keyword_loc, exceptions, operator_loc, reference, statements, consequent, location) end
#deconstruct
Alias for #child_nodes.
# File 'lib/prism/node.rb', line 16291
alias deconstruct child_nodes
#deconstruct_keys(keys)
# File 'lib/prism/node.rb', line 16294
def deconstruct_keys(keys) { keyword_loc: keyword_loc, exceptions: exceptions, operator_loc: operator_loc, reference: reference, statements: statements, consequent: consequent, location: location } end
#inspect
def inspect -> String
# File 'lib/prism/node.rb', line 16341
def inspect InspectVisitor.compose(self) end
#keyword
def keyword: () -> String
# File 'lib/prism/node.rb', line 16331
def keyword keyword_loc.slice end
#keyword_loc
attr_reader keyword_loc: Location
#operator
def operator: () -> String?
# File 'lib/prism/node.rb', line 16336
def operator operator_loc&.slice end
#operator_loc
attr_reader operator_loc: Location?
#type
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
# File 'lib/prism/node.rb', line 16359
def type :rescue_node end