From: James Coglan Date: 2008-09-03T16:15:27+09:00 Subject: Re: How can I prevent include one module multi-times? ------=_Part_18304_29201463.1220426455931 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 2008/9/3 James Coglan > > > 2008/9/3 Thomas B. > >> 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. > I should have mentioned that including a module multiple times will not have any nasty side effects on the inheritance tree of the including class -- this is checked for you. ------=_Part_18304_29201463.1220426455931--