Sha256: d646557bf28cf2987a35ccabcb514017f5e3a16976ed3335308a202be19d2932

Contents?: true

Size: 1.04 KB

Versions: 6

Compression:

Stored size: 1.04 KB

Contents

class Object #module Kernel

  unless method_defined?(:instance_exec) # 1.9

    module InstanceExecMethods #:nodoc:
    end

    include InstanceExecMethods

    # Evaluate the block with the given arguments within the context of
    # this object, so self is set to the method receiver.
    #
    # From Mauricio's http://eigenclass.org/hiki/bounded+space+instance_exec
    #
    # This version has been borrowed from Rails for compatibility sake.
    def instance_exec(*args, &block)
      begin
        old_critical, Thread.critical = Thread.critical, true
        n = 0
        n += 1 while respond_to?(method_name = "__instance_exec#{n}")
        InstanceExecMethods.module_eval { define_method(method_name, &block) }
      ensure
        Thread.critical = old_critical
      end

      begin
        send(method_name, *args)
      ensure
        InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil
      end
    end

    # OLD VERSION
    #def instance_exec(*arguments, &block)
    #  block.bind(self)[*arguments]
    #end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/kernel/instance_exec.rb
facets-2.8.3 lib/core/facets/kernel/instance_exec.rb
facets-2.8.2 lib/core/facets/kernel/instance_exec.rb
facets-2.8.1 lib/core/facets/kernel/instance_exec.rb
facets-2.8.0 lib/core/facets/kernel/instance_exec.rb
facets-2.7.0 lib/core/facets/kernel/instance_exec.rb