# CHANGE 2006-12-27 trans
# * Deprecated use of TemporaryMethods module

require 'thread'

class Proc

  # Bind a proc to an object. Returns a Method.
  #
  # The block's #to_s method (same as #inspect) is
  # used for the temporary method label defined in
  # the Object class.

  def bind(object=nil)
    object ||= eval("self", self)
    block = self
    store = Object
    begin
      old, Thread.critical = Thread.critical, true
      #n=0; n+=1 while store.method_defined?(name="_bind_#{n}")
      name="_bind_#{block}"
      store.module_eval do
        define_method name, &block
      end
      return object.method(name)
    ensure
      store.module_eval do
        remove_method name #rescue nil
        #undef_method name #rescue nil
      end
      Thread.critical = old
    end
  end

end
