Sha256: 713958e761b9d35256c53a6bad4c3f365ab257cf0b77dc10aecae281ce99e55e

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

# http://blog.jayfields.com/2006/09/ruby-instanceexec-aka-instanceeval.html # hints on usage
# http://blog.jayfields.com/2008/02/ruby-dynamically-define-method.html # write up on usage
# http://eigenclass.org/hiki.rb?instance_exec # more study
# http://eigenclass.org/hiki.rb?bounded+space+instance_exec # fix for mem leak final used here
# http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l23
# http://www.jroller.com/abstractScope/entry/passing_parameters_to_an_instance
# instance_exec is used for face methods it allows you to pass args and block to be evaled.
# it works like instance eval but instance eval does not accept arguments
if VERSION <= '1.8.6'
  class Object
    module InstanceExecHelper; end
    include InstanceExecHelper
    # instance_exec method evaluates a block of code relative to the specified object, with parameters whom come from outside the object.
    def instance_exec(*args, &block)
      begin
        old_critical, Thread.critical = Thread.critical, true
        n = 0
        n += 1 while respond_to?(mname="__instance_exec#{n}")
        InstanceExecHelper.module_eval{ define_method(mname, &block) }
      ensure
        Thread.critical = old_critical
      end
      begin
        ret = send(mname, *args)
      ensure
        InstanceExecHelper.module_eval{ remove_method(mname) } rescue nil
      end
      ret
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
watirloo-0.0.8 lib/watirloo/extension/object.rb
watirloo-0.0.7 lib/watirloo/extension/object.rb