Spec-Zone.ru › Ruby 3.3

класс Prism::IfNode

Родитель:
Prism::Node

Представляет использование ключевого слова «if», либо в блочном, либо в модификаторном формате.

bar if foo
^^^^^^^^^^

if foo then bar end
^^^^^^^^^^^^^^^^^^^

Атрибуты

consequent[R]

attr_reader consequent: Node?

end_keyword_loc[R]

attr_reader end_keyword_loc: Location?

if_keyword_loc[R]

attr_reader if_keyword_loc: Location?

predicate[R]

attr_reader predicate: Node

statements[R]

attr_reader statements: StatementsNode?

then_keyword_loc[R]

attr_reader then_keyword_loc: Location?

Методы публичного класса

new(if_keyword_loc, predicate, then_keyword_loc, statements, consequent, end_keyword_loc, location) Показать исходный код
# File lib/prism/node.rb, line 7733
def initialize(if_keyword_loc, predicate, then_keyword_loc, statements, consequent, end_keyword_loc, location)
  @if_keyword_loc = if_keyword_loc
  @predicate = predicate
  @then_keyword_loc = then_keyword_loc
  @statements = statements
  @consequent = consequent
  @end_keyword_loc = end_keyword_loc
  @location = location
end

def initialize: (if_keyword_loc: Location?, predicate: Node, then_keyword_loc: Location?, statements: StatementsNode?, consequent: Node?, end_keyword_loc: Location?, location: Location) -> void

type() Показать исходный код
# File lib/prism/node.rb, line 7854
def self.type
  :if_node
end

Аналогично type, этот метод возвращает символ, который можно использовать для разделения по типу узла, не прибегая к длинной цепочке ===. Обратите внимание, что, как и type, он по-прежнему будет медленнее, чем использование == для одного класса, но должен быть быстрее в операторе case или сравнении массивов.

def self.type: () -> Symbol

Методы публичного экземпляра

accept(visitor) Показать исходный код
# File lib/prism/node.rb, line 7744
def accept(visitor)
  visitor.visit_if_node(self)
end

def accept: (visitor: Visitor) -> void

child_nodes() Показать исходный код
# File lib/prism/node.rb, line 7753
def child_nodes
  [predicate, statements, consequent]
end

def child_nodes: () -> Array[nil | Node]

Также алиасировано как: deconstruct
comment_targets() Показать исходный код
# File lib/prism/node.rb, line 7767
def comment_targets
  [*if_keyword_loc, predicate, *then_keyword_loc, *statements, *consequent, *end_keyword_loc]
end

def comment_targets: () -> Array[Node | Location]

compact_child_nodes() Показать исходный код
# File lib/prism/node.rb, line 7758
def compact_child_nodes
  compact = []
  compact << predicate
  compact << statements if statements
  compact << consequent if consequent
  compact
end

def compact_child_nodes: () -> Массив

copy(**params) Показать исходный код
# File lib/prism/node.rb, line 7772
def copy(**params)
  IfNode.new(
    params.fetch(:if_keyword_loc) { if_keyword_loc },
    params.fetch(:predicate) { predicate },
    params.fetch(:then_keyword_loc) { then_keyword_loc },
    params.fetch(:statements) { statements },
    params.fetch(:consequent) { consequent },
    params.fetch(:end_keyword_loc) { end_keyword_loc },
    params.fetch(:location) { location },
  )
end

def copy: (**params) -> IfNode

deconstruct()

def deconstruct: () -> Array[nil | Node]

Псевдоним для: child_nodes
deconstruct_keys(keys) Показать исходный код
# File lib/prism/node.rb, line 7788
def deconstruct_keys(keys)
  { if_keyword_loc: if_keyword_loc, predicate: predicate, then_keyword_loc: then_keyword_loc, statements: statements, consequent: consequent, end_keyword_loc: end_keyword_loc, location: location }
end

def deconstruct_keys: (keys: Массив) -> Словарь[Символ, nil | Node | Массив | String | Token | Массив | Location]

end_keyword() Показать исходный код
# File lib/prism/node.rb, line 7803
def end_keyword
  end_keyword_loc&.slice
end

def end_keyword: () -> String?

if_keyword() Показать исходный код
# File lib/prism/node.rb, line 7793
def if_keyword
  if_keyword_loc&.slice
end

def if_keyword: () -> String?

inspect(inspector = NodeInspector.new) Показать исходный код
# File lib/prism/node.rb, line 7808
def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── if_keyword_loc: #{inspector.location(if_keyword_loc)}\n"
  inspector << "├── predicate:\n"
  inspector << inspector.child_node(predicate, "│   ")
  inspector << "├── then_keyword_loc: #{inspector.location(then_keyword_loc)}\n"
  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 << "└── end_keyword_loc: #{inspector.location(end_keyword_loc)}\n"
  inspector.to_str
end

def inspect(inspector: NodeInspector) -> String

then_keyword() Показать исходный код
# File lib/prism/node.rb, line 7798
def then_keyword
  then_keyword_loc&.slice
end

def then_keyword: () -> String?

type() Показать исходный код
# File lib/prism/node.rb, line 7844
def type
  :if_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.

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API