James Coglan
9/3/2008 7:14:00 AM
[Note: parts of this message were removed to make it a legal post.]
2008/9/3 Thomas B. <tpreal@gmail.com>
> Zhao Yi wrote:
> > I am looking for a method to check whether this module has been
> > included. Does Ruby have this feature?
>
> What's your question? In the subject you ask 'How can I prevent include
> one module multi-times?' and this is checked automatically without any
> special handling.
That's partially correct, but you'll find that the module's included() hook
gets called repeatedly:
module M
def self.included(base)
puts "included M"
end
end
class C
# prints "included M" 3 times
include M
include M
include M
end
To check whether M is already mixed into C, the expression 'M > C' returns
true if C includes M. You could put this check inside M.included and throw
an exception if it's true.