Difference between include and extend in Ruby

include: inject the module into the target class or module as an instance method

extend: inject the module into the target class or module as a class method

 

Class extension mixins can be implemented through extend

 

Class extension mixins can be done through the following steps

1. Define a module, MyMixin

2. Define an internal module in Mymixin, usually called ClassMethods, and define some methods for it. These methods will eventually become class methods

3. Override the Mymixin#included() method to extend the includes with ClassMethods (using the extend() method)

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326697588&siteId=291194637