Class: Proc

Inherits:
Object show all
Defined in:
lib/instance_exec.rb

Overview

Add a bind method to the Proc class.

Instance Method Summary collapse

Instance Method Details

#bind(object) ⇒ Object

This creates an UnboundMethod (from a temporary instance method) and binds it to a given object. In the case of Object#instance_exec (below), the method is bound to self.



11
12
13
14
15
16
17
18
19
20
# File 'lib/instance_exec.rb', line 11

def bind(object)
  block, time = self, Time.now
  (class << object; self end).class_eval do
    method_name = "__bind_#{time.to_i}_#{time.usec}"
    define_method(method_name, &block)
    method = instance_method(method_name)
    remove_method(method_name)
    method
  end.bind(object)
end