Modules are similar to classes basically every class object is ancestrally a module object.When you create a class you can create instance of that class through which you can execute class's method but this is not possible in modules as it doesn't have instances in place of which it got "MIXIN" by using it ruby indirectly supports multiple inheritance (When a class can inherit features from more than one parent class, the class is supposed to show multiple inheritance)
A class can include a module so that when the class is instantiated it gets the included modules goodies and the method from the included module becomes instance methods in the class that includes the module this is called mixing in and a module is referred to as mixin.
To understand it well see this example
module Cricket
def bat
end
def wickets
end
end
module Badminton
def racket
end
def shuttlecock
end
end
class Eg
include Cricket
include Badminton
def eg1
end
end
samp=Eg.new //creating object
samp.bat
samp.wickets
samp.racket
samp.shuttlecock
samp.eg1
Module Cricket consists of the methods bat and wickets. Module Badminton consists of the methods racket and shuttlecock. The class Eg includes both modules Cricket and Badminton. The class Eg can access all four methods bat,wickets,racket and shuttlecock. Therefore, you can see that the class Eg inherits from both the modules. Thus, you can say the class Eg shows multiple inheritance or a mixin.
0 Comment(s)