class Prism::IfNode
Represents the use of the if keyword, either in the block form or the modifier form, or a ternary expression.
bar if foo ^^^^^^^^^^ if foo then bar end ^^^^^^^^^^^^^^^^^^^ foo ? bar : baz ^^^^^^^^^^^^^^^
Public Class Methods
Source
# File lib/prism/node.rb, line 11523 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
Initialize a new IfNode node.
Public Instance Methods
Source
# File lib/prism/node_ext.rb, line 494 def consequent deprecated("subsequent") subsequent end
Returns the subsequent if/elsif/else clause of the if node. This method is deprecated in favor of subsequent.
Source
# File lib/prism/node.rb, line 11653 def predicate @predicate end
The node for the condition the IfNode is testing.
if foo
^^^
bar
end
bar if foo
^^^
foo ? bar : baz
^^^
Source
# File lib/prism/node.rb, line 11697 def statements @statements end
Represents the body of statements that will be executed when the predicate is evaluated as truthy. Will be nil when no body is provided.
if foo bar ^^^ baz ^^^ end
Source
# File lib/prism/node.rb, line 11717 def subsequent @subsequent end
Locations
Public Instance Methods
Source
# File lib/prism/node.rb, line 11731 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
The Location of the end keyword if present, nil otherwise.
if foo bar end ^^^
Source
# File lib/prism/node.rb, line 11620 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
The Location of the if keyword if present.
bar if foo
^^
The if_keyword_loc field will be nil when the IfNode represents a ternary expression.
Source
# File lib/prism/node.rb, line 11668 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
The Location of the then keyword (if present) or the ? in a ternary expression, nil otherwise.
if foo then bar end
^^^^
a ? b : c
^
Node Interface
Public Class Methods
Public Instance Methods
Source
# File lib/prism/node.rb, line 11548 def accept(visitor) visitor.visit_if_node(self) end
See Node.accept.
Source
# File lib/prism/node.rb, line 11553 def child_nodes [predicate, statements, subsequent] end
See Node.child_nodes.
Source
# File lib/prism/node.rb, line 11576 def comment_targets [*if_keyword_loc, predicate, *then_keyword_loc, *statements, *subsequent, *end_keyword_loc] #: Array[Prism::node | Location] end
See Node.comment_targets.
Source
# File lib/prism/node.rb, line 11567 def compact_child_nodes compact = [] #: Array[Prism::node] compact << predicate compact << statements if statements compact << subsequent if subsequent compact end
Source
# File lib/prism/node.rb, line 11584 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
Creates a copy of self with the given fields, using self as the template.
Source
# File lib/prism/node.rb, line 11558 def each_child_node return to_enum(:each_child_node) unless block_given? yield predicate yield statements if statements yield subsequent if subsequent end
See Node.each_child_node.
Repository
Public Instance Methods
Source
# File lib/prism/node.rb, line 11746 def save_end_keyword_loc(repository) repository.enter(node_id, :end_keyword_loc) unless @end_keyword_loc.nil? end
Save the end_keyword_loc location using the given saved source so that it can be retrieved later.
Source
# File lib/prism/node.rb, line 11635 def save_if_keyword_loc(repository) repository.enter(node_id, :if_keyword_loc) unless @if_keyword_loc.nil? end
Save the if_keyword_loc location using the given saved source so that it can be retrieved later.
Source
# File lib/prism/node.rb, line 11683 def save_then_keyword_loc(repository) repository.enter(node_id, :then_keyword_loc) unless @then_keyword_loc.nil? end
Save the then_keyword_loc location using the given saved source so that it can be retrieved later.
Slicing
Public Instance Methods
Source
# File lib/prism/node.rb, line 11771 def end_keyword end_keyword_loc&.slice end
Slice the location of end_keyword_loc from the source.
Source
# File lib/prism/node.rb, line 11755 def if_keyword if_keyword_loc&.slice end
Slice the location of if_keyword_loc from the source.
Source
# File lib/prism/node.rb, line 11763 def then_keyword then_keyword_loc&.slice end
Slice the location of then_keyword_loc from the source.