From: Peter Date: 2005-05-17T22:01:05+09:00 Subject: Re: Extending an Instance only Once > I have a method that requires that the arguments passed to it have been > in-mixed with a module. Currently, my code looks like this: > > def my_method( the_obj ) > the_obj.extend( MyModule ) unless the_obj.inherits_from?( MyModule ) > end Ruby already checks this: irb(main):001:0> module M ; end => nil irb(main):002:0> class A ; end => nil irb(main):003:0> class B < A; end => nil irb(main):004:0> class A ; include M ; end => A irb(main):005:0> class B ; include M ; end => B irb(main):006:0> B.ancestors => [B, A, M, Object, Kernel] Note that there is a single M in there. If the includes happen in the reverse order, Ruby's check fails and there are two M's in the list: irb(main):001:0> module M ; end => nil irb(main):002:0> class A ; end => nil irb(main):003:0> class B < A; end => nil irb(main):004:0> class B ; include M ; end => B irb(main):005:0> class A ; include M ; end => A irb(main):006:0> B.ancestors => [B, M, A, M, Object, Kernel] This does not proof the check is actually done, but in the source you can see that check is actually there without doubt. The same goes for Object#extend because it is implemented as an include in the singleton class (or selfclass as some people prefer :) which obeys the same rules as includes in regular classes. So no need to check this yourself! Peter