From: Mike Papper Date: 2009-12-08T06:26:33+09:00 Subject: Re: Use of extend from within a Class Thanks for the clarification and sample. So, I cannot simply extend the ActionController::UrlWriter module in my class because that module does this: self.class.default_url_options.merge(options) However, within a rails controller, this module can be included as a "helper". Does anyone know how the magic rails helper function makes this call to self.class.default_url work? I've looked at the code for helper but cannot really make any sense of it. Mike ... > The normal pattern is > > module SomeModule > # define any instance methods here > > module ClassMethods > # define any class methods here > end > > # and here is the crucial step this gets called when some class > # includes the module > def self.included(some_class) > some_class.extend(ClassMethods) > end > end > > So if you want to make a module which adds only class methods either > > 1) define the module 'normally' and in the class which wants the > methods as class methods > > class Foo > extend MyGreatModuleOfClassMethods > end > > or > > 2) Use the pattern above, leave the instance methods out, and put all > of the methods in the inner ClassMethods module (actually the name of > this module is irrelevant except for clarity. then in the using class > > class Foo > include MyGreatModuleWhichOnlyHasClassMethodsRightNow > end > > HTH > > > > > -- > Rick DeNatale > > Blog: http://talklikeaduck.denhaven2.com/ > Twitter: http://twitter.com/RickDeNatale > WWR: http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn: http://www.linkedin.com/in/rickdenatale -- Posted via http://www.ruby-forum.com/.