From: David Masover Date: 2009-02-12T09:20:27+09:00 Subject: Re: Ruby vs Perl performance Michal Suchanek wrote: > On 09/02/2009, David Masover wrote: > > >> Either way, I would put the burden back on you. Why is this so dangerous? >> Why is it any more dangerous than the other duck typing tricks Rubyists use >> every day? Why shouldn't I be able to do: >> >> a.method(:foo).unbind.bind(b) >> >> when a and b aren't related, but I happen to know they share a common >> theme? After all, what ties the method to the object -- isn't it mostly >> going to be calling instance methods, and occasionally accessing instance >> variables -- so why should 'self' be exempted from the "quacks like" rule? >> >> > > Actually you sort of can except the binding is not permanent. > No, I don't think so. I've tried in a method somewhat similar to yours, and I get the same results. > module Kernel > That's probably why. Everything includes Kernel, so if you grab an UnboundMethod from Kernel, you should be able to apply it everywhere. Try this: module Foo def foo :foo #ok, I'm unimaginative end end Foo.instance_method(:foo).bind(Object.new).call See what happens? Doesn't matter whether Foo is a class or a module, you can only bind methods of it to objects which are somehow its descendants. If it's a module, you can only do this to things which have included that module, making it somewhat less useful. Speaking of which, modifying Kernel -- yet another thing more dangerous than letting UnboundMethods bind to anything.