Skip to content

Instantly share code, notes, and snippets.

@mallipeddi
Created August 5, 2010 16:18
Show Gist options
  • Select an option

  • Save mallipeddi/509966 to your computer and use it in GitHub Desktop.

Select an option

Save mallipeddi/509966 to your computer and use it in GitHub Desktop.
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"
@KranthiKishore
Copy link

Can you tell me what exactly is this bound and unbound method? I really dont understand this concept.. Can you please explain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment