Find file
Fetching contributors…
Cannot retrieve contributors at this time
39 lines (26 sloc) 779 Bytes
= force_bind
Adds UnboundMethod#force_bind to bind an unbound method to class (or any object of any type).
It basically bypasses the argument type checking that #bind has.
== Requirements
* Ruby 1.9.1 or greater (does not work in Ruby 1.8.x)
== Example
Bind an instance method to its class (making it act like a class method):
class Foo
def bar
puts "I'm inside #{self}"
#=> "I'm inside #<Foo:0x123456>"
meth = Foo.instance_method(:bar).force_bind(Foo)
#=> "I'm inside Foo"
You can also use this to rebind the instance method of any object to any
other object:
class A
def foo; self end
class B; end
meth = A.instance_method(:foo).force_bind(
#=> #<B:0x123456>