Monday, 3 June 2013

include vs extend in Ruby

    include : Mixes in specified module methods as instance methods in the target class
    extend : Mixes in specified module methods as class methods in the target class
   Example :-
    module ReusableModule
      def module_method
        puts "Module Method: Hi there!"

    class ClassThatIncludes
      include ReusableModule
    class ClassThatExtends
      extend ReusableModule

    puts "Include"
    => "Module Method: Hi there!"

    puts "Extend"
    => "Module Method: Hi there!"
    If you include module ReusableModule in class ClassThatIncludes, the methods, constants, classes, submodules, and other declarations gets referenced.

    include is a private method, because it's intended to be called from within the container class/module.

    If you extend class ClassThatExtends with module ReusableModule, then the methods and constants gets copied. Obviously, if you are not careful, you can waste a lot of memory by dynamically duplicating definitions.

    extend is a public method.

    extend - adds the specified module's methods and constants to the target's metaclass (i.e. the singleton class. For ex :

    * if you call Klazz.extend(Mod), now Klazz has Mod's methods (as class methods)
    * if you call obj.extend(Mod), now obj has Mod's methods (as instance methods), but no other instance of of obj.class has those methods.
    extend is a public method