Created
August 5, 2010 16:18
-
-
Save mallipeddi/509966 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Foo | |
| def foo | |
| puts "fofo" | |
| end | |
| end | |
| # Module#instance_method | |
| # => returns an UnboundMethod object | |
| unbound_foo = Foo.instance_method(:foo) | |
| p unbound_foo # => #<UnboundMethod: Foo#foo> | |
| # Object#method | |
| # => returns a BoundMethod object (bound to a specific instance) | |
| f1 = Foo.new | |
| p f1.method(:foo) # => #<Method: Foo#foo> | |
| f2 = Foo.new | |
| p f1.method(:foo) == f2.method(:foo) # => false | |
| # BoundMethod#unbind | |
| # => returns the unbound version of the BoundMethod | |
| p f1.method(:foo).unbind # => #<UnboundMethod: Foo#foo> | |
| # UnBoundMethod#bind | |
| # => returns the bound version of the UnboundMethod (bound to a specific instance) | |
| # => Note: this instance has to be kind_of?(unbound-method's-original-class) | |
| p unbound_foo.bind(Foo.new).call # => #<Method: Foo#foo> --> "fofo" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you tell me what exactly is this bound and unbound method? I really dont understand this concept.. Can you please explain