Method#unbind
returns an UnboundMethod
reference to the method, which can later be bound to another object using UnboundMethod#bind
.
class Foo
attr_reader :baz
def initialize(baz)
@baz = baz
end
end
class Bar
def initialize(baz)
@baz = baz
end
end
f = Foo.new(:test1)
g = Foo.new(:test2)
h = Bar.new(:test3)
f.method(:baz).unbind.bind(g).call # => :test2
f.method(:baz).unbind.bind(h).call # => TypeError: bind argument must be an instance of Foo
Initially, I thought this is incredibly awesome, because I expected it would work similarly to JavaScript's Function.prototype.call()
/Function.prototype.apply()
. However, the object to which you want to bind the method must be of the same class.
The only application I can think of is if you unbind a method, lose the original implementation (redefine the method in the original or singleton class) and then rebind and call it.