Class: Peruby::Op::Conditional
Overview
Conditional branch operation.
Instance Method Summary collapse
-
#initialize(condition, then_branch, else_branch) ⇒ Conditional
constructor
A new instance of Conditional.
- #run(env, context) ⇒ Object
- #to_callable ⇒ Object
Methods inherited from Base
#argument_cells, #local_slot, #lvalue, #run_subroutine, #warning_directive?
Constructor Details
#initialize(condition, then_branch, else_branch) ⇒ Conditional
Returns a new instance of Conditional.
106 107 108 109 110 |
# File 'lib/peruby/op/control.rb', line 106 def initialize(condition, then_branch, else_branch) @condition = condition @then_branch = then_branch @else_branch = else_branch end |
Instance Method Details
#run(env, context) ⇒ Object
112 113 114 115 |
# File 'lib/peruby/op/control.rb', line 112 def run(env, context) branch = Conv.truthy?(@condition.run(env, :scalar)) ? @then_branch : @else_branch branch&.run(env, context) end |
#to_callable ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/peruby/op/control.rb', line 117 def to_callable condition = @condition.to_callable scalar_condition = @condition.scalar_callable if @condition.respond_to?(:scalar_callable) then_branch = @then_branch.to_callable else_branch = @else_branch&.to_callable lambda do |env, context| value = scalar_condition ? scalar_condition.call(env) : condition.call(env, :scalar) branch = Conv.truthy?(value) ? then_branch : else_branch branch&.call(env, context) end end |