Sha256: 5898afb62230772f9fe6bc4c664cd045f8e3817af02c60865b8566064a4ac510

Contents?: true

Size: 1010 Bytes

Versions: 9

Compression:

Stored size: 1010 Bytes

Contents

require 'brass'

require 'facets/dir/ascend'

class Object

  unless method_defined?(:instance_exec) # 1.9
    require 'thread'

    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

  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
qed-2.8.8 lib/qed/core_ext.rb
qed-2.8.7 lib/qed/core_ext.rb
qed-2.8.6 lib/qed/core_ext.rb
qed-2.8.5 lib/qed/core_ext.rb
qed-2.8.4 lib/qed/core_ext.rb
qed-2.8.3 lib/qed/core_ext.rb
qed-2.8.2 lib/qed/core_ext.rb
qed-2.8.1 lib/qed/core_ext.rb
qed-2.8.0 lib/qed/core_ext.rb