Daniel Berger blogged about how to
make 'include' a little more flexible
by allowing to rename or exclude specific methods.
He proposed the following syntax:
module TFoo
def method_a
"hello"
end
def method_b
"world"
end
end
module TBar
def method_a
"goodbye"
end
def method_b
"cruel world"
end
def method_y
"huh?"
end
end
class MyKlass
include TFoo,
:alias => {:method_a, :method_z},
:exclude => :method_b
include TBar
end
m = MyKlass.new
m.method_a
m.method_b
m.method_y
m.method_z
Making it happen
Here's my implementation. I think somebody might have posted such a
thing to ruby-talk, but anyway this must have been written for the first
time by some Japanese Rubyist years ago, so yet another reinvention
won't matter nor hurt.
This definition of Class#include should be a
fairly safe replacement for the default one since I'm not raising an
exception when there's a nameclash (which I rather feel is a bad idea,
but I haven't given this much thought), but maybe it could make sense to
create a new method with those semantics.
The code:
Read more...
Read: Renaming and picking methods from included modules---tastes like Eiffel?