Z
Zhao Yi
I am looking for a method to check whether this module has been
included. Does Ruby have this feature?
included. Does Ruby have this feature?
Zhao said:I am looking for a method to check whether this module has been
included. Does Ruby have this feature?
2008/9/3 Thomas B. said: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.
2008/9/3 James Coglan said: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.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.