Forcing Kernel::method_name to be called in Ruby
- by Peter
I want to add a foo method to Ruby's Kernel module, so I can write foo(obj) anywhere and have it do something to obj. Sometimes I want a class to override foo, so I do this:
module Kernel
private # important; this is what Ruby does for commands like 'puts', etc.
def foo x
if x.respond_to? :foo
x.foo # use overwritten method.
else
# do something to x.
end
end
end
this is good, and works. but, what if I want to use the default Kernel::foo in some other object that overwrites foo? Since I've got an instance method foo, I've lost the original binding to Kernel::foo.
class Bar
def foo # override behaviour of Kernel::foo for Bar objects.
foo(3) # calls Bar::foo, not the desired call of Kernel::foo.
Kernel::foo(3) # can't call Kernel::foo because it's private.
# question: how do I call Kernel::foo on 3?
end
end
Is there any clean way to get around this? I'd rather not have two different names, and I definitely don't want to make Kernel::foo public.