class SyntaxTree::IfOp

IfOp represents a ternary clause.

predicate ? truthy : falsy

Attributes

comments[R]
Array[ Comment | EmbDoc ]

the comments attached to this node

falsy[R]
Node

the expression to be executed if the predicate is falsy

predicate[R]
Node

the expression to be checked

truthy[R]
Node

the expression to be executed if the predicate is truthy

Public Class Methods

new(predicate:, truthy:, falsy:, location:) click to toggle source
# File lib/syntax_tree/node.rb, line 6557
def initialize(predicate:, truthy:, falsy:, location:)
  @predicate = predicate
  @truthy = truthy
  @falsy = falsy
  @location = location
  @comments = []
end

Public Instance Methods

===(other) click to toggle source
# File lib/syntax_tree/node.rb, line 6631
def ===(other)
  other.is_a?(IfOp) && predicate === other.predicate &&
    truthy === other.truthy && falsy === other.falsy
end
accept(visitor) click to toggle source
# File lib/syntax_tree/node.rb, line 6565
def accept(visitor)
  visitor.visit_if_op(self)
end
child_nodes() click to toggle source
# File lib/syntax_tree/node.rb, line 6569
def child_nodes
  [predicate, truthy, falsy]
end
Also aliased as: deconstruct
copy(predicate: nil, truthy: nil, falsy: nil, location: nil) click to toggle source
# File lib/syntax_tree/node.rb, line 6573
def copy(predicate: nil, truthy: nil, falsy: nil, location: nil)
  node =
    IfOp.new(
      predicate: predicate || self.predicate,
      truthy: truthy || self.truthy,
      falsy: falsy || self.falsy,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end
deconstruct()
Alias for: child_nodes
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/node.rb, line 6588
def deconstruct_keys(_keys)
  {
    predicate: predicate,
    truthy: truthy,
    falsy: falsy,
    location: location,
    comments: comments
  }
end
format(q) click to toggle source
# File lib/syntax_tree/node.rb, line 6598
def format(q)
  force_flat = [
    AliasNode,
    Assign,
    Break,
    Command,
    CommandCall,
    Heredoc,
    IfNode,
    IfOp,
    Lambda,
    MAssign,
    Next,
    OpAssign,
    RescueMod,
    ReturnNode,
    Super,
    Undef,
    UnlessNode,
    VoidStmt,
    YieldNode,
    ZSuper
  ]

  if q.parent.is_a?(Paren) || force_flat.include?(truthy.class) ||
       force_flat.include?(falsy.class)
    q.group { format_flat(q) }
    return
  end

  q.group { q.if_break { format_break(q) }.if_flat { format_flat(q) } }
end