From: Rick DeNatale Date: 2009-12-08T08:35:48+09:00 Subject: Re: Use of extend from within a Class On Mon, Dec 7, 2009 at 4:26 PM, Mike Papper wrote: > 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. MyController < ActionController::Base include UrlWriter # include not extend end If you read the code in actionpack/lib/action_controller/helpers.rb it really isn't that hard to see what's going on if in the controller you write say helper :url_writer then the helper method takes the case branch for a symbol. which is: file_name = arg.to_s.underscore + '_helper' class_name = file_name.camelize begin require_dependency(file_name) rescue LoadError => load_error requiree = / -- (.*?)(\.rb)?$/.match(load_error.message).to_a[1] if requiree == file_name msg = "Missing helper file helpers/#{file_name}.rb" raise LoadError.new(msg).copy_blame!(load_error) else raise end end add_template_helper(class_name.constantize) So it computes a file name from the argument, in this case "url_writer.rb", and a class (actually a Module) name "UrlWriter" Then require dependency loads the file if necessary, and finally. class_name.constantize turns teh class name into the actual module, and passes it as the argument to add_template_helper which is defined above as: def add_template_helper(helper_module) #:nodoc: master_helper_module.module_eval { include helper_module } end So it is effectively the same thing as what I first wrote. -- 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