Class: Luoma::TernaryExpression
- Inherits:
-
Expression
- Object
- Expression
- Luoma::TernaryExpression
- Defined in:
- lib/luoma/expression.rb,
sig/luoma/expression.rbs
Instance Attribute Summary
Attributes inherited from Expression
Instance Method Summary collapse
- #children ⇒ Object
- #evaluate(context) ⇒ Object
-
#initialize(token, consequence, condition, alternative) ⇒ TernaryExpression
constructor
(t_token, Expression, Expression, Expression?) -> void.
- #to_s ⇒ Object
Methods inherited from Expression
Constructor Details
#initialize(token, consequence, condition, alternative) ⇒ TernaryExpression
(t_token, Expression, Expression, Expression?) -> void
98 99 100 101 102 103 104 |
# File 'lib/luoma/expression.rb', line 98 def initialize(token, consequence, condition, alternative) super(token) @consequence = consequence @condition = condition @alternative = alternative @span = Luoma.span(@consequence.token, (@alternative || @condition).span) end |
Instance Method Details
#children ⇒ Object
115 116 117 |
# File 'lib/luoma/expression.rb', line 115 def children [@consequence, @condition, @alternative].compact end |
#evaluate(context) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/luoma/expression.rb', line 107 def evaluate(context) if context.env.truthy?(@condition.evaluate(context), context) @consequence.evaluate(context) else @alternative&.evaluate(context) || :nothing end end |
#to_s ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/luoma/expression.rb', line 120 def to_s if @alternative "#{@consequence} if #{@condition} else #{@alternative}" else "#{@consequence} if #{@condition}" end end |