Here’s a practical example.
class MyClass
include MyModule
end
When you will include the module in a class, the included hook will be called. Therefore, second_class_method will be called within the scope of Class.
What happens here is
-
first_methodandsecond_methodare included as instance-methods ofMyClass.instance = MyClass.new instance.first_method # => whatever returned value of first_method is -
The methods of
ClassMethodsare automatically mixed as class methods ofMyClass. This is a common Ruby pattern, thatActiveSupport::Concernencapsulates. The non-Rails Ruby code ismodule MyModule def self.included(base) base.extend ClassMethods end module ClassMethods def this_is_a_class_method end end endWhich results in
MyClass.this_is_a_class_methodor in your case
MyClass.first_class_method -
includedis a hook that is effectively to the following code# non-Rails version module MyModule def self.included(base) base.class_eval do # somecode end end end # Rails version with ActiveSupport::Concerns module MyModule included do # somecode end endIt’s mostly “syntactic sugar” for common patterns. What happens in practice, is that when you mix the module, that code is executed in the context of the mixer class.