for example, the rubygem Devise has the following piece of code in lib/devise/controllers/helpers.rb
module Helpers
extend ActiveSupport::Concern
why use extend here? Will include do the same?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No,
includewill not do the same.extendandincludeperform similar but distinct roles.includetakes the instance methods of the included module and makes them available to instances of the including module. In effect,includeinserts the included module as a superclass of the including (in fact,#ancestorswill even show the included module).extend, on the other hand, adds the methods of the named module to the receiver. In the case of callingextendduring a module definition, that means that the “extended” module’s instance methods will become class methods of the “extending” module. It’s often used to import decorators (really just calls to class methods) into a class or module being defined.So, in brief, the snippet above will take the instance methods of
ActiveSupport::Concernand make them class methods ofHelpers.