My question is similar to "What is the difference between include and extend in Ruby?".
What's the difference between require
and include
in Ruby? If I just want to use the methods from a module in my class, should I require
it or include
it?
require
and include
, and also relevant, Module#append_features
.
What's the difference between "include" and "require" in Ruby? Answer: The include and require methods do very different things. The require method does what include does in most other programming languages: run another file. It also tracks what you've required in the past and won't require the same file twice. To run another file without this added functionality, you can use the load method. The include method takes all the methods from another module and includes them into the current module. This is a language-level thing as opposed to a file-level thing as with require. The include method is the primary way to "extend" classes with other modules (usually referred to as mix-ins). For example, if your class defines the method "each", you can include the mixin module Enumerable and it can act as a collection. This can be confusing as the include verb is used very differently in other languages.
So if you just want to use a module, rather than extend it or do a mix-in, then you'll want to use require
.
Oddly enough, Ruby's require
is analogous to C's include
, while Ruby's include
is almost nothing like C's include
.
If you're using a module, that means you're bringing all the methods into your class. If you extend
a class with a module, that means you're "bringing in" the module's methods as class methods. If you include
a class with a module, that means you're "bringing in" the module's methods as instance methods.
EX:
module A
def say
puts "this is module A"
end
end
class B
include A
end
class C
extend A
end
B.say
=> undefined method 'say' for B:Class
B.new.say
=> this is module A
C.say
=> this is module A
C.new.say
=> undefined method 'say' for C:Class
require
and include
are.
From the Metaprogramming Ruby book,
The require() method is quite similar to load(), but it’s meant for a different purpose. You use load() to execute code, and you use require() to import libraries.
Ruby require is more like "include" in other languages (such as C). It tells Ruby that you want to bring in the contents of another file. Similar mechanisms in other languages are: using
using
import
Ruby include is an object-oriented inheritance mechanism used for mixins.
There is a good explanation here:
[The] simple answer is that require and include are essentially unrelated. "require" is similar to the C include, which may cause newbie confusion. (One notable difference is that locals inside the required file "evaporate" when the require is done.) The Ruby include is nothing like the C include. The include statement "mixes in" a module into a class. It's a limited form of multiple inheritance. An included module literally bestows an "is-a" relationship on the thing including it.
Emphasis added.
include
here. Namespaces, packages and class-typed objects can all be treated as specialized environments which hold name bindings in general. (Consider these differences and Kernel's $import!
. Also notes Racket's environments are implemented in namespaces.)
From Programming Ruby 1.9
We’ll make a couple of points about the include statement before we go on. First, it has nothing to do with files. C programmers use a preprocessor directive called #include to insert the contents of one file into another during compilation. The Ruby include statement simply makes a reference to a module. If that module is in a separate file, you must use require (or its less commonly used cousin, load) to drag that file in before using include. Second, a Ruby include does not simply copy the module’s instance methods into the class. Instead, it makes a reference from the class to the included module. If multiple classes include that module, they’ll all point to the same thing. If you change the definition of a method within a module, even while your program is running, all classes that include that module will exhibit the new behavior.
Have you ever tried to require
a module? What were the results? Just try:
MyModule = Module.new
require MyModule # see what happens
Modules cannot be required, only included!
TypeError
) if the word Module
is changed to Class
.
'Load'- inserts a file's contents.(Parse file every time the file is being called)
'Require'- inserts a file parsed content.(File parsed once and stored in memory)
'Include'- includes the module into the class and can use methods inside the module as class's instance method
'Extend'- includes the module into the class and can use methods inside the module as class method
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 would need the same code within the module.
Load The load method is almost like the require method except 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.
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.
You can prefer this http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/
Below are few basic differences between require and include:
Require:
Require reads the file from the file system, parses it, saves to the memory and runs it in a given place which means if you will even change anything while the script is running than that change will not reflect. We require file by name, not by module name. It is typically used for libraries and extensions.
Include:
When you include a module into your class it behaves as if you took the code defined in your module and inserted it in your class. We include module name, not the file name. It is typically used to dry up the code and to remove duplication in the code.
require(name)
It will return bolean true/false
The name which is passed as parameter to the require, ruby will try to find the source file with that name in your load path. 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. So it keeps track of whether that library was already loaded or not.
include module_name
Suppose if you have some methods that you need to have in two different classes. Then you don't have to write them in both the classes. Instead what you can do is, define it in module. And then include this module in other classes. It is provided by Ruby just to ensure DRY principle. It’s used to DRY up your code to avoid duplication
Success story sharing
require
simply runs the file. Period. In particular, this means that if you put therequire
call inside a class declaration, the code in the file will not become a part of that class, if you put therequire
call inside a method definition, the code in the file will not become a part of that method, if you put therequire
call inside a module declaration, the code in the file will not become part of that module, and so on and so forth. It doesn't even become part of the file therequire
call is in. There's plenty of question here on Stack Overflow that are …include
in Ruby is simply inheritance.class C; include M end
makesM
the superclass ofC
and the (former) superclass ofC
the superclass ofM
. I don't think that C'sinclude
works via inheritance, I think it is simple textual/lexical substitution.