Class: Prism::RescueModifierNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::RescueModifierNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents an expression modified with a rescue.
foo rescue nil
^^^^^^^^^^^^^^
Instance Attribute Summary collapse
-
#expression ⇒ Object
readonly
attr_reader expression: Node.
-
#keyword_loc ⇒ Object
readonly
attr_reader keyword_loc: Location.
-
#rescue_expression ⇒ Object
readonly
attr_reader rescue_expression: Node.
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) -> RescueModifierNode.
- #deconstruct_keys(keys) ⇒ Object
-
#initialize(expression, keyword_loc, rescue_expression, location) ⇒ RescueModifierNode
constructor
def initialize: (expression: Node, keyword_loc: Location, rescue_expression: Node, location: Location) -> void.
- #inspect(inspector = NodeInspector.new) ⇒ Object
-
#keyword ⇒ Object
def keyword: () -> 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(expression, keyword_loc, rescue_expression, location) ⇒ RescueModifierNode
def initialize: (expression: Node, keyword_loc: Location, rescue_expression: Node, location: Location) -> void
12096 12097 12098 12099 12100 12101 |
# File 'lib/prism/node.rb', line 12096 def initialize(expression, keyword_loc, rescue_expression, location) @expression = expression @keyword_loc = keyword_loc @rescue_expression = rescue_expression @location = location end |
Instance Attribute Details
#expression ⇒ Object (readonly)
attr_reader expression: Node
12087 12088 12089 |
# File 'lib/prism/node.rb', line 12087 def expression @expression end |
#keyword_loc ⇒ Object (readonly)
attr_reader keyword_loc: Location
12090 12091 12092 |
# File 'lib/prism/node.rb', line 12090 def keyword_loc @keyword_loc end |
#rescue_expression ⇒ Object (readonly)
attr_reader rescue_expression: Node
12093 12094 12095 |
# File 'lib/prism/node.rb', line 12093 def rescue_expression @rescue_expression end |
Instance Method Details
#accept(visitor) ⇒ Object
def accept: (visitor: Visitor) -> void
12104 12105 12106 |
# File 'lib/prism/node.rb', line 12104 def accept(visitor) visitor.visit_rescue_modifier_node(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
def child_nodes: () -> Array[nil | Node]
12113 12114 12115 |
# File 'lib/prism/node.rb', line 12113 def child_nodes [expression, rescue_expression] end |
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location]
12123 12124 12125 |
# File 'lib/prism/node.rb', line 12123 def comment_targets [expression, keyword_loc, rescue_expression] end |
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array
12118 12119 12120 |
# File 'lib/prism/node.rb', line 12118 def compact_child_nodes [expression, rescue_expression] end |
#copy(**params) ⇒ Object
def copy: (**params) -> RescueModifierNode
12128 12129 12130 12131 12132 12133 12134 12135 |
# File 'lib/prism/node.rb', line 12128 def copy(**params) RescueModifierNode.new( params.fetch(:expression) { expression }, params.fetch(:keyword_loc) { keyword_loc }, params.fetch(:rescue_expression) { rescue_expression }, params.fetch(:location) { location }, ) end |
#deconstruct_keys(keys) ⇒ Object
12141 12142 12143 |
# File 'lib/prism/node.rb', line 12141 def deconstruct_keys(keys) { expression: expression, keyword_loc: keyword_loc, rescue_expression: rescue_expression, location: location } end |
#inspect(inspector = NodeInspector.new) ⇒ Object
12150 12151 12152 12153 12154 12155 12156 12157 12158 |
# File 'lib/prism/node.rb', line 12150 def inspect(inspector = NodeInspector.new) inspector << inspector.header(self) inspector << "├── expression:\n" inspector << inspector.child_node(expression, "│ ") inspector << "├── keyword_loc: #{inspector.location(keyword_loc)}\n" inspector << "└── rescue_expression:\n" inspector << inspector.child_node(rescue_expression, " ") inspector.to_str end |
#keyword ⇒ Object
def keyword: () -> String
12146 12147 12148 |
# File 'lib/prism/node.rb', line 12146 def keyword keyword_loc.slice end |
#set_newline_flag(newline_marked) ⇒ Object
12108 12109 12110 |
# File 'lib/prism/node.rb', line 12108 def set_newline_flag(newline_marked) expression.set_newline_flag(newline_marked) 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
12174 12175 12176 |
# File 'lib/prism/node.rb', line 12174 def type :rescue_modifier_node end |