For the same reason that Peter mentioned, I would like to add an example so that new developers can easily understand self.included (base) and self.extended (base):
module Module1 def fun1 puts "fun1 from Module1" end def self.included(base) def fun2 puts "fun2 from Module1" end end end module Module2 def foo puts "foo from Module2" end def self.extended(base) def bar puts "bar from Module2" end end end class Test include Module1 extend Module2 def abc puts "abc form Test" end end
Test.new.abC # => abc form Test
Test.new.fun1 # => fun1 from module 1
Test.new.fun2 # => fun2 from Module1
Test.foo # => foo from Module2
Test.bar # => bar from module 2
extend: methods will be available as class methods
include: methods will be available as instance methods
"base" in self.extended (base) /self.included (base):
The base parameter in the static extended method will be either an instance object or an object of a class of this class that extended the module depending on whether you are expanding the object or class accordingly.
When the class includes the module, the self.included method will be activated. The base parameter will be the class object for the class, which includes the module.
FaaduBaalak Jan 03 '17 at 5:34 on 2017-01-03 05:34
source share