Skip to content

Instantly share code, notes, and snippets.

@mattyoho
Created November 5, 2009 05:38
Show Gist options
  • Save mattyoho/226799 to your computer and use it in GitHub Desktop.
Save mattyoho/226799 to your computer and use it in GitHub Desktop.
class Proc #:nodoc:
def bind(object)
block, time = self, Time.now
(class << object; self end).class_eval do
methodname = "bind#{time.to_i}_#{time.usec}"
definemethod(methodname, &block)
method = instancemethod(methodname)
removemethod(methodname)
method
end.bind(object)
end
end
#Here’s how it works:
# Defines a method on self’s (a Proc) singleton class using the Proc itself as the method implementation
# Grabs a handle to the UnboundMethod representing the Proc
# Removes the method from the Proc’s singleton class (but we still have the UnboundMethod from step 2)
# Binds the UnboundMethod to the object passed to Proc#bind, returning a Method object
# http://evan.tiggerpalace.com/articles/2009/11/04/procbind-with-bobby-wilson/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment