Trans
7/22/2006 2:26:00 PM
I've been bitten by this way too many times now. And I'm just plain
tired of it.
Today's case:
def wrap_method( sym, &blk )
raise ArgumentError, "method does not exist -- #{sym}" unless
method_defined?( sym )
old = instance_method(sym)
define_method(sym) { |*args| blk.call(old.bind(self), *args) }
end
Seems all well and good until now when it comes out that
#method_defined?, like #instance_methods, does something only a Ruby
hacker without a life would expect. The actual _monster_ code needed
is:
def wrap_method( sym, &blk )
unless public_method_defined?( sym ) || private_method_defined?(
sym ) || protected_method_defined?( sym )
raise ArgumentError, "method does not exist -- #{sym}"
end
old = instance_method(sym)
define_method(sym) { |*args| blk.call(old.bind(self), *args) }
end
Now, it just doesn't stand to any good reason. When we wan't to
actually grab a method, we don't use #public_instance_method, do we?
That would be a nightmare! And so it is with the above case and many
others I've delt with. Every time I have to go back and fix this "bug"
recalling the terribly inconvenient and anti-literal definition of
these methods-methods.
T.