Class: Peruby::Op::Ternary

Inherits:
Base
  • Object
show all
Defined in:
lib/peruby/op/expression.rb

Overview

Lazy conditional expression.

Instance Method Summary collapse

Methods inherited from Base

#argument_cells, #local_slot, #lvalue, #run_subroutine, #warning_directive?

Constructor Details

#initialize(condition, true_expression, false_expression) ⇒ Ternary

Returns a new instance of Ternary.



320
321
322
323
324
# File 'lib/peruby/op/expression.rb', line 320

def initialize(condition, true_expression, false_expression)
  @condition = condition
  @true_expression = true_expression
  @false_expression = false_expression
end

Instance Method Details

#run(env, context) ⇒ Object



326
327
328
329
# File 'lib/peruby/op/expression.rb', line 326

def run(env, context)
  branch = Conv.truthy?(@condition.run(env, :scalar)) ? @true_expression : @false_expression
  branch.run(env, context)
end

#to_callableObject



331
332
333
334
335
336
337
338
339
# File 'lib/peruby/op/expression.rb', line 331

def to_callable
  condition = @condition.to_callable
  true_expression = @true_expression.to_callable
  false_expression = @false_expression.to_callable
  lambda do |env, context|
    branch = Conv.truthy?(condition.call(env, :scalar)) ? true_expression : false_expression
    branch.call(env, context)
  end
end