Class: SyntaxTree::IfOp
Overview
IfOp represents a ternary clause.
predicate ? truthy : falsy
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#falsy ⇒ Object
readonly
- untyped
-
the expression to be executed if the predicate is falsy.
-
#predicate ⇒ Object
readonly
- untyped
-
the expression to be checked.
-
#truthy ⇒ Object
readonly
- untyped
-
the expression to be executed if the predicate is truthy.
Attributes inherited from Node
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(predicate:, truthy:, falsy:, location:, comments: []) ⇒ IfOp
constructor
A new instance of IfOp.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(predicate:, truthy:, falsy:, location:, comments: []) ⇒ IfOp
Returns a new instance of IfOp.
5445 5446 5447 5448 5449 5450 5451 |
# File 'lib/syntax_tree/node.rb', line 5445 def initialize(predicate:, truthy:, falsy:, location:, comments: []) @predicate = predicate @truthy = truthy @falsy = falsy @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
5443 5444 5445 |
# File 'lib/syntax_tree/node.rb', line 5443 def comments @comments end |
#falsy ⇒ Object (readonly)
- untyped
-
the expression to be executed if the predicate is falsy
5440 5441 5442 |
# File 'lib/syntax_tree/node.rb', line 5440 def falsy @falsy end |
#predicate ⇒ Object (readonly)
- untyped
-
the expression to be checked
5434 5435 5436 |
# File 'lib/syntax_tree/node.rb', line 5434 def predicate @predicate end |
#truthy ⇒ Object (readonly)
- untyped
-
the expression to be executed if the predicate is truthy
5437 5438 5439 |
# File 'lib/syntax_tree/node.rb', line 5437 def truthy @truthy end |
Instance Method Details
#accept(visitor) ⇒ Object
5453 5454 5455 |
# File 'lib/syntax_tree/node.rb', line 5453 def accept(visitor) visitor.visit_if_op(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
5457 5458 5459 |
# File 'lib/syntax_tree/node.rb', line 5457 def child_nodes [predicate, truthy, falsy] end |
#deconstruct_keys(_keys) ⇒ Object
5463 5464 5465 5466 5467 5468 5469 5470 5471 |
# File 'lib/syntax_tree/node.rb', line 5463 def deconstruct_keys(_keys) { predicate: predicate, truthy: truthy, falsy: falsy, location: location, comments: comments } end |
#format(q) ⇒ Object
5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 |
# File 'lib/syntax_tree/node.rb', line 5473 def format(q) force_flat = [ Alias, Assign, Break, Command, CommandCall, Heredoc, If, IfMod, IfOp, Lambda, MAssign, Next, OpAssign, RescueMod, Return, Return0, Super, Undef, Unless, UnlessMod, UntilMod, VarAlias, VoidStmt, WhileMod, Yield, Yield0, 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 |