Ruby require vs load vs include vs extend
In general:
Require vs load: require is loaded only once, and load may be loaded multiple times.
Include vs extend: the methods in the include module are the instance methods of the class, and the extend is the class methods.
Here are the differences between require, load, include and extend Methods: Include
When you include a module into your class as shown below, it's as if you took the Code defined within the module and inserted it within the class, where you 'include 'It. it allows the 'mixin' behavior. it's used to dry up your code to avoid duplication, for instance, if there were multiple classes that wowould need the same code within the module.
The following assumes that the module log and class testclass are defined in the same. RB file. if they were in separate files, then 'load' or 'require 'must be used to let the class know about the module you 've defined.
Module log def class_type "this class is of Type: # {self. Class}" endend class testclass include log #... end Tc = testclass. New. class_type
The above will print "this class is of Type: testclass"
Load
The load method is almost like the require method doesn't it doesn't keep track of whether or not that library has been loaded. so it's possible to load a library multiple times and also when using the load method you must specify the ". RB "extension of the Library file name.
Most of the time, you'll want to use require instead of load but load is there if you want a library to be loaded each time load is called. for example, if your module changes its State frequently, you may want to use load to pick up those changes within classes loaded from.
Here's an example of how to use load. place the load method at the very top of your ". RB "file. also the load method takes a path to the file as an argument:
Load 'test _ Library. rb'
So for example, if the module is defined in a separate. RB file than it's used, then you can use
File: log. Rb
Module log def class_type "this class is of Type: # {self. Class}" endend
File: test. Rb
Load 'Log. rb' class testclass include log #... 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 after the first time. the require method only needs to be used if library you are loading is defined in a separate file, which is usually the case.
So it keeps track of whether that library was already loaded or not. You also don't need to specify the ". RB" extension of the Library file name.
Here's an example of how to use require. Place the require method at the very top of your ". RB" file:
Require 'test _ library'
Extend
When using the extend method instead of include, you are adding the module's methods as class methods instead of as instance methods.
Here is an example of how to use the extend method:
Module log def class_type "this class is of Type: # {self. Class}" endend class testclass extend log #... end Tc = testclass. class_type
The above will print "this class is of Type: testclass"
When using extend instead of include within the class, if you try to instantiate testclass and call method class_type on it, as you did in the include example above, you'll get a nomethoderror. so, again, the module's methods become available as class methods.