Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "self.included(base)" mean in a module?

Tags:

ruby

I see this pattern a lot:

module Article::Score

    def self.included(base)
        base.send :extend, ClassMethods
        base.send :include, InstanceMethods
    end

    module ClassMethods
    ...
    end

    module InstanceMethods
    ...
    end
end

Then in the article model, I see this

class Article
   include Article::Score
   ...
end

so my guess is that "base" probably refers to the article class and we're just including the instance methods and extending the class methods. But can someone explain the snippet "self.included(base)" and give an overview of what's going on there?

like image 312
User314159 Avatar asked Mar 21 '23 22:03

User314159


1 Answers

The self.included function is called when the module is included. It allows methods to be executed in the context of the base (where the module is included).

like image 136
PPPPPPPPP Avatar answered Apr 09 '23 02:04

PPPPPPPPP