Class: Proc

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/proc_ext.rb

Instance Method Summary collapse

Instance Method Details

#bind(object) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/core_ext/proc_ext.rb', line 2

def bind(object)
  block = self
  object.class_eval do
    method_name = :__proc_rebound_method__
    method = nil
    Thread.exclusive do
      method_already_exists =
        object.respond_to?(method_name) &&
        instance_method(method_name).owner == self

      old_method = instance_method(method_name) if method_already_exists

      define_method(method_name, &block)
      method = instance_method(method_name)
      remove_method(method_name)

      define_method(method_name, old_method) if method_already_exists
    end
    method
  end.bind(object)
end