I'm not precisely sure of a workaround other than remove_method
, but the reason why what you've shown doesn't work is the way that Ruby performs method lookup.
Inspecting Foo
's ancestors
during each step of Foo
's creation gives us a big hint:
module One
def test; puts 'Test One'; end
end
module Two
def test; puts 'Test Two'; end
end
class Foo
include One
p ancestors
include Two
p ancestors
include One
p ancestors
end
Output:
[Foo, One, Object, Kernel]
[Foo, Two, One, Object, Kernel]
[Foo, Two, One, Object, Kernel]
If a module is already in a class's ancestry, Ruby doesn't allow you to re-include it again. Thus, when Two
is included after One
, Two
occurs in Foo
's lookup table before One
has a chance, no matter how many times you re-include One
.