# Require, load and include --- what's the difference?

**URL:** <https://www.ruby-forum.com/t/require-load-and-include-whats-the-difference/79651>\
**Category:** Ruby\
**Created:** [December 17, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/require-load-and-include-whats-the-difference/79651 "2006-12-17T18:20:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [December 17, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/require-load-and-include-whats-the-difference/79651/1 "2006-12-17T18:20:38Z")

</div>

Hello,

I’m browsing through some code, trying to familiarize myself with the  
language (I have a C++ background, with only a #include), and I realize  
that there are 3 calls that I’m trying to wrap my head around.

Here’s my stab:

Require – seems to include another file to gain access to other  
classes, modules, and global data. This seems to be equivalent to a C++  
include, with inclusion guards

Load – seems to be the same as require, but will actually reload the  
data every time it is called, where require will load once and cache the  
file. This seems to be equivalent to C++ include, with out inclusion  
guards.

Include – seems to include a module name from a pre-determined path,  
but not classes (unless encapsulated within a module). Would this be  
similar to a C++ namespace being given the include directory at compile  
time?

So a couple of things here, are my understandings of the 3 methods  
close, if not, could someone please expand on them. The other is what’s  
the advantage of include vs require? I mean, why would I include a  
module, rather than require it’s filename?

I’ve read through the RDocs, but I still don’t seem to understand.

I hope this post makes sense, let me know if I need to clarify.

Thanks

Matt

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [December 17, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/require-load-and-include-whats-the-difference/79651/2 "2006-12-17T18:52:45Z")

</div>

On 12/17/06, matt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> include, with inclusion guards  
> Right

> Load – seems to be the same as require, but will actually reload the  
> data every time it is called, where require will load once and cache the  
> file. This seems to be equivalent to C++ include, with out inclusion  
> guards.

Right

> Include – seems to include a module name from a pre-determined path,  
> but not classes (unless encapsulated within a module). Would this be  
> similar to a C++ namespace being given the include directory at compile  
> time?

include mixes in a module, so the methods defined on the module become  
instance methods on the class.

module Foo  
def foo; “foo”; end  
end

class Boring  
include Foo  
end

Boring.new.foo =\> “foo”

> So a couple of things here, are my understandings of the 3 methods  
> close, if not, could someone please expand on them. The other is what’s  
> the advantage of include vs require? I mean, why would I include a  
> module, rather than require it’s filename?

load and require are for loading a file into your code. include is  
used to actually enhance an individual object (because a class itself  
is an object).

Pat

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [December 17, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/require-load-and-include-whats-the-difference/79651/3 "2006-12-17T18:56:47Z")

</div>

On 12/17/06, matt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,

Hi,

> Load – seems to be the same as require, but will actually reload the  
> data every time it is called, where require will load once and cache the  
> file. This seems to be equivalent to C++ include, with out inclusion  
> guards.

Seems fine to me with the difference that #include is evaluated at  
compile time whereas load, require works at run-time (there is no  
compile-time in Ruby).

> Include – seems to include a module name from a pre-determined path,  
> but not classes (unless encapsulated within a module). Would this be  
> similar to a C++ namespace being given the include directory at compile  
> time?

You should see include as the ruby approach to multiple inheritance.  
Include mix in a class the method defined in the given module. This  
way you can share methods between different classes.

> So a couple of things here, are my understandings of the 3 methods  
> close, if not, could someone please expand on them. The other is what’s  
> the advantage of include vs require? I mean, why would I include a  
> module, rather than require it’s filename?

require works on a file. It tells ruby to load a file if it is not  
already loaded. It is closer to the source shell builtin than the  
#include cpp directory from my point of view.  
include adds the methods defined in a module into a class.

I hope this can help you.

Cheers,

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [December 17, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/require-load-and-include-whats-the-difference/79651/4 "2006-12-17T19:00:48Z")

</div>

I think you’ve got it right.

require basically copies and pastes the referenced file into the source,  
but it only includes classes and modules, I don’t think variables  
besides instance variables and class variables get imported.

include makes it so you don’t have to use the scope resolution operator  
“::”. It “unwraps” a module is how I like to think of it.

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [December 17, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/require-load-and-include-whats-the-difference/79651/5 "2006-12-17T19:04:49Z")

</div>

On Dec 17, 9:19 am, matt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Require – seems to include another file to gain access to other  
> classes, modules, and global data. This seems to be equivalent to a C++  
> include, with inclusion guards
> 
> Load – seems to be the same as require, but will actually reload the  
> data every time it is called, where require will load once and cache the  
> file. This seems to be equivalent to C++ include, with out inclusion  
> guards.

Basically correct on these two.

> Include – seems to include a module name from a pre-determined path,  
> but not classes (unless encapsulated within a module). Would this be  
> similar to a C++ namespace being given the include directory at compile  
> time?

> what’s  
> the advantage of include vs require? I mean, why would I include a  
> module, rather than require it’s filename?

Include doesn’t really have anything to do with files or paths. For  
instance, the following code could all be in the same file:

## BEGIN

Woden:~ jwilger$ irb

> > module Foo  
> > def hello  
> > “defined in module Foo”  
> > end  
> > end  
> > =\> nil

?\> class Bar

> > include Foo  
> > end  
> > =\> Bar

?\> x = Bar.new  
=\> #Bar:0x12a1838

> > x.hello  
> > =\> “defined in module Foo”

## END

When used this way, modules and include are Ruby’s preferred way to  
implement that which would be achieved via multiple inheritance in some  
other languages.

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [December 17, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/require-load-and-include-whats-the-difference/79651/6 "2006-12-17T19:04:50Z")

</div>

matt wrote:

> include, with inclusion guards
> 
> Load – seems to be the same as require, but will actually reload the  
> data every time it is called, where require will load once and cache the  
> file. This seems to be equivalent to C++ include, with out inclusion  
> guards.

You got the point. There’s a difference however, as you’re including  
directly the code and not the header as is done usually in C++.  
Basically, you’ll only ever need to use require, unless you need some  
special kind of interaction with the (programmer-)user.

> Include – seems to include a module name from a pre-determined path,  
> but not classes (unless encapsulated within a module). Would this be  
> similar to a C++ namespace being given the include directory at compile  
> time?

I don’t really understand what you mean about the include directory.  
Module serves two purposes: namespace separation and mixins. Mixins are  
Ruby’s version of multiple inheritance: whereas C++ authorize multiple  
parents for classes, Ruby forbids this, but provides a mechanism for  
classes that share common capabilities to share code as well. Let’s make  
this more explicit:

Array is a descendant of Object, as are all Ruby classes. Hash is also  
a descendant of Object. But both share some common capabilities, such as  
they all provide a collection of Objects you might like to go through.  
This is where modules come in: both include the Enumerable module, which  
provides facilities to traverse collections of objects.

In C++, the design would be different:

class Object {};  
class Enumerable {};  
class Hash: public Object, Enumerable {};  
class Array: public Object, Enumerable {};

In ruby,  
class Array  
include Enumerable  
end

class Hash  
include Enumerable  
end

There you go.

> So a couple of things here, are my understandings of the 3 methods  
> close, if not, could someone please expand on them. The other is what’s  
> the advantage of include vs require? I mean, why would I include a  
> module, rather than require it’s filename?

You need to require the filename first so that the module is  
available. Then, you can include it in classes if you need their  
functionality.

Cheers,

```
Vince
```
