Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • what is actually module and mixin is?

    • 0
    • 3
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 285
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: