class Prism::IfNode
Представляет использование ключевого слова if в блочной или модификаторной форме либо тернарное выражение.
bar if foo ^^^^^^^^^^ if foo then bar end ^^^^^^^^^^^^^^^^^^^ foo ? bar : baz ^^^^^^^^^^^^^^^
Атрибуты
Узел условия, которое проверяет IfNode.
if foo
^^^
bar
end
bar if foo
^^^
foo ? bar : baz
^^^ Представляет тело операторов, которые будут выполнены, если предикат будет оценен как истинный. Будет иметь значение nil, если тело не указано.
if foo bar ^^^ baz ^^^ end
Открытые методы класса
# File lib/prism/node.rb, line 8549 def initialize(source, node_id, location, flags, if_keyword_loc, predicate, then_keyword_loc, statements, subsequent, end_keyword_loc) @source = source @node_id = node_id @location = location @flags = flags @if_keyword_loc = if_keyword_loc @predicate = predicate @then_keyword_loc = then_keyword_loc @statements = statements @subsequent = subsequent @end_keyword_loc = end_keyword_loc end
Инициализирует новый узел IfNode.
# File lib/prism/node.rb, line 8737 def self.type :if_node end
Возвращает символьное представление типа этого узла. См. Node::type.
Общедоступные методы экземпляра
# File lib/prism/node.rb, line 8743
def ===(other)
other.is_a?(IfNode) &&
(if_keyword_loc.nil? == other.if_keyword_loc.nil?) &&
(predicate === other.predicate) &&
(then_keyword_loc.nil? == other.then_keyword_loc.nil?) &&
(statements === other.statements) &&
(subsequent === other.subsequent) &&
(end_keyword_loc.nil? == other.end_keyword_loc.nil?)
end Реализует проверку равенства по case для узла. По сути, это ==, но без сравнения значений расположений. Проверяется только наличие расположений.
# File lib/prism/node.rb, line 8563 def accept(visitor) visitor.visit_if_node(self) end
def accept: (Visitor visitor) -> void
# File lib/prism/node.rb, line 8568 def child_nodes [predicate, statements, subsequent] end
def child_nodes: () -> Array
# File lib/prism/node.rb, line 8582 def comment_targets [*if_keyword_loc, predicate, *then_keyword_loc, *statements, *subsequent, *end_keyword_loc] #: Array[Prism::node | Location] end
def comment_targets: () -> Array[Node | Location]
# File lib/prism/node.rb, line 8573 def compact_child_nodes compact = [] #: Array[Prism::node] compact << predicate compact << statements if statements compact << subsequent if subsequent compact end
def compact_child_nodes: () -> Array
# File lib/prism/node_ext.rb, line 488
def consequent
deprecated("subsequent")
subsequent
end Возвращает последующую ветвь if/elsif/else узла if. Этот метод устарел; вместо него следует использовать subsequent.
# File lib/prism/node.rb, line 8587 def copy(node_id: self.node_id, location: self.location, flags: self.flags, if_keyword_loc: self.if_keyword_loc, predicate: self.predicate, then_keyword_loc: self.then_keyword_loc, statements: self.statements, subsequent: self.subsequent, end_keyword_loc: self.end_keyword_loc) IfNode.new(source, node_id, location, flags, if_keyword_loc, predicate, then_keyword_loc, statements, subsequent, end_keyword_loc) end
def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?if_keyword_loc: Location?, ?predicate: Prism::node, ?then_keyword_loc: Location?, ?statements: StatementsNode?, ?subsequent: ElseNode | IfNode | nil, ?end_keyword_loc: Location?) -> IfNode
# File lib/prism/node.rb, line 8595
def deconstruct_keys(keys)
{ node_id: node_id, location: location, if_keyword_loc: if_keyword_loc, predicate: predicate, then_keyword_loc: then_keyword_loc, statements: statements, subsequent: subsequent, end_keyword_loc: end_keyword_loc }
end def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, if_keyword_loc: Location?, predicate: Prism::node, then_keyword_loc: Location?, statements: StatementsNode?, subsequent: ElseNode | IfNode | nil, end_keyword_loc: Location? }
# File lib/prism/node.rb, line 8722 def end_keyword end_keyword_loc&.slice end
def end_keyword: () -> String?
# File lib/prism/node.rb, line 8693
def end_keyword_loc
location = @end_keyword_loc
case location
when nil
nil
when Location
location
else
@end_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end
end Расположение ключевого слова end, если оно присутствует; в противном случае — nil.
if foo bar end ^^^
# File lib/prism/node.rb, line 8712 def if_keyword if_keyword_loc&.slice end
def if_keyword: () -> String?
# File lib/prism/node.rb, line 8605
def if_keyword_loc
location = @if_keyword_loc
case location
when nil
nil
when Location
location
else
@if_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end
end Расположение ключевого слова if, если оно присутствует.
bar if foo
^^ Поле if_keyword_loc будет nil, если IfNode представляет тернарное выражение.
# File lib/prism/node.rb, line 8727 def inspect InspectVisitor.compose(self) end
def inspect -> String
# File lib/prism/node.rb, line 8707 def save_end_keyword_loc(repository) repository.enter(node_id, :end_keyword_loc) unless @end_keyword_loc.nil? end
Сохранить расположение end_keyword_loc, используя указанный сохранённый исходный код, чтобы его можно было получить позже.
# File lib/prism/node.rb, line 8619 def save_if_keyword_loc(repository) repository.enter(node_id, :if_keyword_loc) unless @if_keyword_loc.nil? end
Сохранить расположение if_keyword_loc, используя указанный сохранённый исходный код, чтобы его можно было получить позже.
# File lib/prism/node.rb, line 8658 def save_then_keyword_loc(repository) repository.enter(node_id, :then_keyword_loc) unless @then_keyword_loc.nil? end
Сохранить расположение then_keyword_loc, используя указанный сохранённый исходный код, чтобы его можно было получить позже.
# File lib/prism/node.rb, line 8717 def then_keyword then_keyword_loc&.slice end
def then_keyword: () -> String?
# File lib/prism/node.rb, line 8644
def then_keyword_loc
location = @then_keyword_loc
case location
when nil
nil
when Location
location
else
@then_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end
end Расположение ключевого слова then (если оно присутствует) или ? в тернарном выражении; в противном случае — nil.
if foo then bar end
^^^^
a ? b : c
^ # File lib/prism/node.rb, line 8732 def type :if_node end
Возвращает символьное представление типа этого узла. См. Node#type.
Ruby Core © 1993–2025 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.