From: James Edward Gray II Date: 2010-03-22T05:15:59+09:00 Subject: Re: A plugin system using extend On Mar 21, 2010, at 2:51 PM, Jean-denis Vauguet wrote: > extend is a method of the Object class, and it can handle any object as > a receiver, be it a class, an instance of a class, or a module, a > singleton, etc. If you extend a class, then you'll get class methods. An > instance, instance methods. And what's so great about extend is, once > again, the way it alters the inheritance chain: the class calling extend > for a module gets subclassed by the module-now-a-class. > Quite different is include, a keyword not a method, which has only one > behavior: quoting the Pickaxe, "it makes a reference from the class to > the included module. If multiple classes include that module, they'll > all point to the same thing". So you get shared, instance methods. > So, the real difference between extend and include, aside from their > nature, is not really about whether they're talking to classes or > instances, it's more about their behavior: extend is useful for > redefining things (once), include (mixin) is useful for adding (shared) > things. This isn't totally accurate. extend() is a stupid simple shortcut that really is just an include. This code: obj.extend(Whatever) is identical to: class << obj include Whatever end So it really does all work the same. The reason it moves the methods in front of the main class though is that the singleton class is in front of the main class. Thus including the module behind that class still has them in front of the main class. I talked about this quite a bit in my presentation at LSRC last year: Video: http://lsrc2009.confreaks.com/module-magic-james-edward-gray-ii-28-aug-2009.html Slides: http://grayproductions.net/ruby/module_magic.tar.gz The spirit of what you said is all right on though. James Edward Gray II