Class: Object

Inherits:
BasicObject
Includes:
InstanceExecMethods
Defined in:
lib/instance_exec.rb

Overview

defines Object.instance_exec to permit call of a Proc with params in the context of an instance : instance.instance_exec( foo, bar, &proc ) taken from rails 2.2

Defined Under Namespace

Modules: InstanceExecMethods

Instance Method Summary collapse

Instance Method Details

#instance_exec(*args, &block) ⇒ Object

Evaluate the block with the given arguments within the context of this object, so self is set to the method receiver.

From Mauricio’s eigenclass.org/hiki/bounded+space+instance_exec



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/instance_exec.rb', line 19

def instance_exec(*args, &block)
  method_name = InstanceExecMethods.mutex.synchronize do
    n = 0
    n += 1 while respond_to?(method_name = "__instance_exec#{n}")
    InstanceExecMethods.module_eval { define_method(method_name, &block) }
    method_name
  end

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