Sha256: 67b203bbdadb60de76f66c177f508cc1bac4b3a440b9e74fa5f880624ce0db36

Contents?: true

Size: 1.05 KB

Versions: 30

Compression:

Stored size: 1.05 KB

Contents

class Bindless

  def initialize(bindings = [])
    @bindings = bindings
  end

  def method_missing(m, *args)
    @bindings.reverse_each do |bind|
      begin
        method = eval("method(%s)" % m.inspect, bind)
      rescue NameError
      else
        return method.call(*args)
      end
      begin
        value = eval(m.to_s, bind)
        return value
      rescue NameError
      end
    end
    raise NoMethodError, "No such variable or method: %s" % m
  end

  def pop_binding
    @bindings.pop
  end

  def push_binding(bind)
    @bindings.push bind
  end

  def push_instance(obj)
    @bindings.push obj.instance_eval { binding }
  end

  def push_hash(vars)
    push_instance Struct.new(*vars.keys).new(*vars.values)
  end

  def get_binding
    instance_eval { binding }
  end

  def run_proc(p, *args)
    instance_exec(*args, &p)
  end

  def push_method(name, p, obj=nil)
    x = Object.new
    singleton = class << x; self; end
    singleton.send(:define_method, name, lambda { |*args|
      obj.instance_exec(*args, &p)
    })
    push_instance x
  end

end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
procemon-1.0.0 lib/procemon/function/binding/bindless.rb
procemon-0.9.1 lib/procemon/function/binding/bindless.rb
procemon-0.9.0 lib/procemon/function/binding/bindless.rb
procemon-0.8.1 lib/procemon/function/binding/bindless.rb
procemon-0.8.0 lib/procemon/function/binding/bindless.rb
procemon-0.7.0 lib/procemon/function/binding/bindless.rb
procemon-0.6.3 lib/procemon/function/binding/bindless.rb
procemon-0.6.2 lib/procemon/function/binding/bindless.rb
procemon-0.6.1 lib/procemon/function/binding/bindless.rb
procemon-0.5.0 lib/procemon/function/binding/bindless.rb
procemon-0.4.5 lib/procemon/function/binding/bindless.rb
procemon-0.4.4 lib/procemon/function/binding/bindless.rb
procemon-0.4.3 lib/procemon/function/binding/bindless.rb
procemon-0.4.2 lib/procemon/function/binding/bindless.rb
procemon-0.4.1 lib/procemon/function/binding/bindless.rb
procemon-0.4.0 lib/procemon/function/binding/bindless.rb
procemon-0.3.4 lib/procemon/function/binding/bindless.rb
procemon-0.3.3 lib/procemon/function/binding/bindless.rb
procemon-0.3.2 lib/procemon/function/binding/bindless.rb
procemon-0.3.1 lib/procemon/function/binding/bindless.rb