Created
November 5, 2009 05:38
-
-
Save mattyoho/226799 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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