class Prism::RescueNode
Представляет оператор rescue.
begin rescue Foo, *splat, Bar => ex foo ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ end
‘Foo, *splat, Bar` находятся в поле `exceptions`. `ex` находится в поле `exception`.
Атрибуты
attr_reader consequent: RescueNode?
attr_reader exceptions: Массив
attr_reader keyword_loc: Location
attr_reader operator_loc: Location?
attr_reader reference: Node?
attr_reader statements: StatementsNode?
Публичные методы класса
# File lib/prism/node.rb, line 14784 def initialize(keyword_loc, exceptions, operator_loc, reference, statements, consequent, location) @keyword_loc = keyword_loc @exceptions = exceptions @operator_loc = operator_loc @reference = reference @statements = statements @consequent = consequent @location = location end
def initialize: (keyword_loc: Location, exceptions: Массив, operator_loc: Location?, reference: Node?, statements: StatementsNode?, consequent: RescueNode?, location: Location) -> void
# File lib/prism/node.rb, line 14901 def self.type :rescue_node end
Аналогично type, этот метод возвращает символ, который вы можете использовать для разделения по типу узла, не выполняя длинную цепочку ===. Обратите внимание, что, как и type, он всё ещё будет медленнее, чем использование == для одного класса, но должен быть быстрее в операторе case или сравнении массивов.
def self.type: () -> Symbol
Публичные методы экземпляра
# File lib/prism/node.rb, line 14795 def accept(visitor) visitor.visit_rescue_node(self) end
def accept: (visitor: Visitor) -> void
# File lib/prism/node.rb, line 14800 def child_nodes [*exceptions, reference, statements, consequent] end
def child_nodes: () -> Массив[nil | Узел]
# File lib/prism/node.rb, line 14815 def comment_targets [keyword_loc, *exceptions, *operator_loc, *reference, *statements, *consequent] end
def comment_targets: () -> Массив[Узел | Расположение]
# File lib/prism/node.rb, line 14805 def compact_child_nodes compact = [] compact.concat(exceptions) compact << reference if reference compact << statements if statements compact << consequent if consequent compact end
def compact_child_nodes: () -> Массив
# File lib/prism/node.rb, line 14820
def copy(**params)
RescueNode.new(
params.fetch(:keyword_loc) { keyword_loc },
params.fetch(:exceptions) { exceptions },
params.fetch(:operator_loc) { operator_loc },
params.fetch(:reference) { reference },
params.fetch(:statements) { statements },
params.fetch(:consequent) { consequent },
params.fetch(:location) { location },
)
end def copy: (**params) -> RescueNode
# File lib/prism/node.rb, line 14836
def deconstruct_keys(keys)
{ keyword_loc: keyword_loc, exceptions: exceptions, operator_loc: operator_loc, reference: reference, statements: statements, consequent: consequent, location: location }
end def deconstruct_keys: (keys: Массив) -> Словарь[Символ, nil | Node | Массив | String | Token | Массив | Расположение]
# File lib/prism/node.rb, line 14851
def inspect(inspector = NodeInspector.new)
inspector << inspector.header(self)
inspector << "├── keyword_loc: #{inspector.location(keyword_loc)}\n"
inspector << "├── exceptions: #{inspector.list("#{inspector.prefix}│ ", exceptions)}"
inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n"
if (reference = self.reference).nil?
inspector << "├── reference: ∅\n"
else
inspector << "├── reference:\n"
inspector << reference.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix)
end
if (statements = self.statements).nil?
inspector << "├── statements: ∅\n"
else
inspector << "├── statements:\n"
inspector << statements.inspect(inspector.child_inspector("│ ")).delete_prefix(inspector.prefix)
end
if (consequent = self.consequent).nil?
inspector << "└── consequent: ∅\n"
else
inspector << "└── consequent:\n"
inspector << consequent.inspect(inspector.child_inspector(" ")).delete_prefix(inspector.prefix)
end
inspector.to_str
end def inspect(inspector: NodeInspector) -> String
# File lib/prism/node.rb, line 14841 def keyword keyword_loc.slice end
def keyword: () -> String
# File lib/prism/node.rb, line 14846 def operator operator_loc&.slice end
def operator: () -> String?
# File lib/prism/node.rb, line 14891 def type :rescue_node end
Иногда вам нужно проверить экземпляр узла на соответствие списку классов, чтобы определить, какое поведение выполнить. Обычно это делается с помощью ‘[cls1, cls2].include?(node.class)` или размещения узла в операторе case и выполнения `case node; when cls1; when cls2; end`. Оба этих подхода относительно медленные из-за постоянных проверок, вызовов методов и/или выделения памяти массивов.
Вместо этого, вы можете вызвать type, который вернёт вам символ для сравнения. Это быстрее, чем другие подходы, потому что использует единственное целочисленное сравнение, а также потому, что на CRuby можно воспользоваться тем фактом, что операторы case со всеми символами в качестве ключей будут использовать таблицу переходов.
def type: () -> Symbol
Ruby Core © 1993–2022 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.