Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Differences between Require, Load, Include and Extend methods in ROR

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 457
    Comment on it

    Include: If your application has many classes that need the same code, then we can keep that reusable code in a module and include that module in class.
    When we Include a module into a class, it is like we are taking all the methods within the module and inserting them into the current class, where we include it. So we can say include is a language-level as opposed to require which is file-level. We use include to DRY up the code and avoid duplicacy. It allows the mixin behavior.

    For example:

     module Hello
       def say
         puts "Hello World!"
       end
     end
    
     class A
       include Hello
     end
    
    A.say => undefined method say for a:Class
    
    A.new.say => Hello World!

    In the example above we assumes that the module Hello and class A are defined in the same ".rb" file. If they are in separate files, then we should use load or require. Also including a module means bringing all the module’s methods as instance methods.


    Load: The load method works almost similar to require method but there the difference is only that, load method doesn’t keep track of whether the library has been loaded or not. So it is possible to load a library multiple times. while using the load method we should specify the ".rb" extension of the library file name.
    Mostly we use require but load can be used when a library needs to be loaded each time load is called. For example, if your module is changing frequently, then we should use load update those changes within classes.
    Always place load method at the very top of your file. Also, the load method takes a path to the file as an argument:

    So for example, if the module is defined in a separate .rb file:

    File: hello.rb

    module Hello
       def say
         puts "Hello World!"
       end
     end

     

    File: test.rb

    load 'hello.rb'
     
    class Test
      def say
         puts "In class Test"
       end
       
    end


    Require: The require method allows you to load a library and prevents it from being loaded more than once. The require method will return false if you try to load the same library more than once. The require method only needs to be used if the library you are loading is defined in a separate file.So it keeps track of whether that library was already loaded or not. Also, there is no need to specify the ".rb" extension of the library file name. Always place the required method at the very top of your ".rb" file:

    Example code:

    File: test.rb

    require 'hello'
     
    class Test
      def say
         puts "In class Test"
       end
       
    end

    Extend: When we use the extend method instead of include, i means we are adding the module’s methods as class methods instead of as instance methods.
    For example:

     module Hello
       def say
         puts "Hello World!"
       end
     end
    
     class A
       extend Hello
     end
    
    A.say => Hello World!
    
    A.new.say => undefined method say for a:Class

     

 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: