Class: Peruby::Op::Bind

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

Overview

Explicit =~ or !~ binding.

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(subject, pattern, negated) ⇒ Bind

Returns a new instance of Bind.



98
99
100
101
102
# File 'lib/peruby/op/regexp.rb', line 98

def initialize(subject, pattern, negated)
  @subject = subject
  @pattern = pattern
  @negated = negated
end

Instance Method Details

#run(env, context) ⇒ Object



104
105
106
# File 'lib/peruby/op/regexp.rb', line 104

def run(env, context)
  bind(env, context) { @subject.run(env, :scalar) }
end

#to_callableObject



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/peruby/op/regexp.rb', line 108

def to_callable
  subject = @subject.to_callable
  scalar_subject = @subject.scalar_callable if @subject.respond_to?(:scalar_callable)
  lvalue = @subject.lvalue_callable if @subject.respond_to?(:lvalue_callable)
  match = @pattern.match_callable if !@negated && @pattern.respond_to?(:match_callable)
  lambda do |env, context|
    value = scalar_subject ? scalar_subject.call(env) : subject.call(env, :scalar)
    cell = lvalue&.call(env)
    next match.call(cell ? cell.value : value, env, context) if match

    apply_bound(cell || Scalar.new(value), env, context)
  end
end