# How to avoid multiple inclusion of a file

**URL:** <https://www.ruby-forum.com/t/how-to-avoid-multiple-inclusion-of-a-file/140187>\
**Category:** Ruby\
**Created:** [June 11, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/how-to-avoid-multiple-inclusion-of-a-file/140187 "2008-06-11T18:25:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![suresh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@suresh](https://www.ruby-forum.com/u/suresh)\
**Post date:** [June 11, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/how-to-avoid-multiple-inclusion-of-a-file/140187/1 "2008-06-11T18:25:37Z")

</div>

Hi

My program is split up into multiple files, each containing a set of  
related class definitions. In the main file, all these files are  
included by using require. But how can i avoid multiple inclusion of a  
file by mistake.

Is there anything similar to C languages #ifndef …

thanks  
suresh

---

<div class="post-metadata">

**Author:** ![suresh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@suresh](https://www.ruby-forum.com/u/suresh)\
**Post date:** [June 11, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/how-to-avoid-multiple-inclusion-of-a-file/140187/2 "2008-06-11T18:29:43Z")

</div>

As long as the require lines are exactly the same, there’s nothing to  
worry about.

file1.rb:  
require ‘lib’

file2.rb:  
require ‘lib’

This will work fine. You will run into problems if you put relative  
paths though:

file1.rb:  
require ‘lib’

lower/file3.rb:  
require ‘…/lib’

This will end up require-ing lib.rb twice, and possibly cause problems.

Either use full paths, or add directories to the $LOAD\_PATH to protect  
against this.

Jason

---

<div class="post-metadata">

**Author:** ![suresh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@suresh](https://www.ruby-forum.com/u/suresh)\
**Post date:** [June 11, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/how-to-avoid-multiple-inclusion-of-a-file/140187/3 "2008-06-11T18:36:46Z")

</div>

On Jun 11, 2008, at 10:24 AM, suresh wrote:

> Is there anything similar to C languages #ifndef …

unless defined? A

require ‘a’

end

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![suresh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@suresh](https://www.ruby-forum.com/u/suresh)\
**Post date:** [June 11, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/how-to-avoid-multiple-inclusion-of-a-file/140187/4 "2008-06-11T18:42:05Z")

</div>

On Wed, Jun 11, 2008 at 12:34 PM, suresh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > This will work fine. You will run into problems if you put relative  
> > Either use full paths, or add directories to the $LOAD\_PATH to protect
> > 
> > > file by mistake.  
> > > twice and I had to spend a lot of time to figure out how this  
> > > happened. So thats why my question, how can I avoid multiple inclusion  
> > > of files.
> 
> suresh

I just told you how to avoid it. If you’re getting collisions, then  
you’re doing what I’ve outlined above.

Jason

---

<div class="post-metadata">

**Author:** ![suresh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@suresh](https://www.ruby-forum.com/u/suresh)\
**Post date:** [June 11, 2008, 6:37pm UTC](https://www.ruby-forum.com/t/how-to-avoid-multiple-inclusion-of-a-file/140187/5 "2008-06-11T18:37:22Z")

</div>

On Jun 11, 9:28 pm, Jason R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> paths though:  
> against this.
> 
> > Is there anything similar to C languages #ifndef …
> 
> > thanks  
> > suresh

Hi Jason,

Will tell you what happened to me. I had some database populating code  
in some import class definitions. By mistake that file got included  
twice and I had to spend a lot of time to figure out how this  
happened. So thats why my question, how can I avoid multiple inclusion  
of files.

suresh

---

<div class="post-metadata">

**Author:** ![suresh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@suresh](https://www.ruby-forum.com/u/suresh)\
**Post date:** [June 11, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/how-to-avoid-multiple-inclusion-of-a-file/140187/6 "2008-06-11T18:47:25Z")

</div>

On Jun 11, 2008, at 10:34 AM, suresh wrote:

> Hi Jason,
> 
> Will tell you what happened to me. I had some database populating code  
> in some import class definitions. By mistake that file got included  
> twice and I had to spend a lot of time to figure out how this  
> happened. So thats why my question, how can I avoid multiple inclusion  
> of files.
> 
> suresh

if you are writing the files that are included you can self-protect them

unlss defined? $mylib

module MyLib  
end

end

however, if you expect something like rails or ramaze to autoload  
these files in development mode think twice.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)
