From: Gavin Kistner Date: 2005-05-17T22:45:59+09:00 Subject: Re: Extending an Instance only Once --Apple-Mail-4-591371425 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed On May 17, 2005, at 7:01 AM, Peter wrote: >> 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 > 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! Hrm...I see code that looks like that's what's going on inside of class.c, but apparently it is faster to test myself: require 'benchmark' include Benchmark module M; end class Bar < Array; end class Foo < Bar; end n = 500000 bm(15){ |x| f = Foo.new x.report( "Always Extend" ){ n.times{ x.extend( M ) } } f = Foo.new x.report( "Test First" ){ n.times{ x.extend( M ) unless x.is_a?( M ) } } } user system total real Always Extend 1.610000 0.010000 1.620000 ( 1.983911) Test First 0.730000 0.000000 0.730000 ( 0.745857) Is there a flaw with my test above? --Apple-Mail-4-591371425--