# How to stop a file from being required

**URL:** <https://www.ruby-forum.com/t/how-to-stop-a-file-from-being-required/204018>\
**Category:** Ruby\
**Created:** [March 3, 2011, 2:11pm UTC](https://www.ruby-forum.com/t/how-to-stop-a-file-from-being-required/204018 "2011-03-03T14:11:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 3, 2011, 2:11pm UTC](https://www.ruby-forum.com/t/how-to-stop-a-file-from-being-required/204018/1 "2011-03-03T14:11:16Z")

</div>

Hi Guys,

If i require a file by doing:  
Dir[“features/whitelabel/step\_definitions/\*.rb”].each {|file| require  
file }

is there something I can write before or after this to stop it from  
loading if the file has already been loaded?

Also can I use an IF statement or something?

Any help would be brilliant. At the moment the step\_definitions folder  
is getting loaded twice which is causing me massive headach. So all i  
want to do is just to write a statement to say, if the file is loaded  
do not load it again.

For some reason It is loading twice when it should not be allowed to.

any ideas?

Kind regards,  
Usman H.

---

<div class="post-metadata">

**Author:** ![usmanhhussain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/5fc32e/32.png) [@usmanhhussain](https://www.ruby-forum.com/u/usmanhhussain)\
**Post date:** [March 3, 2011, 2:22pm UTC](https://www.ruby-forum.com/t/how-to-stop-a-file-from-being-required/204018/2 "2011-03-03T14:22:35Z")

</div>

On Thu, Mar 3, 2011 at 1:10 PM, Usman H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> is there something I can write before or after this to stop it from  
> loading if the file has already been loaded?
> 
> […]
> 
> For some reason It is loading twice when it should not be allowed to.

[http://www.ruby-doc.org/core/classes/Kernel.html#M001418](http://www.ruby-doc.org/core/classes/Kernel.html#M001418)

The name of the loaded feature is added to the array in $". A feature  
will

> not be loaded if its name already appears in $".

Perhaps you’re requiring the file using two different paths? I can see  
this  
happening if you change $LOAD\_PATH in one of the required files in a  
lower  
directory.

---

<div class="post-metadata">

**Author:** ![usmanhhussain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/5fc32e/32.png) [@usmanhhussain](https://www.ruby-forum.com/u/usmanhhussain)\
**Post date:** [March 3, 2011, 2:42pm UTC](https://www.ruby-forum.com/t/how-to-stop-a-file-from-being-required/204018/3 "2011-03-03T14:42:40Z")

</div>

The only place this gets tricky is if you load a file from two different  
paths. So, a test\_helper.rb for example

$ ls  
test\_helper.rb  
some\_test.rb  
foo  
|  
`foo\_test.rb

In some\_test:

require ‘test\_helper’

in foo\_test

require ‘…/test\_helper’

Now it’ll load the test\_helper twice, because those two strings are  
different. The solution:

require “File.expand\_path(test\_helper)”

---

<div class="post-metadata">

**Author:** ![usmanhhussain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/5fc32e/32.png) [@usmanhhussain](https://www.ruby-forum.com/u/usmanhhussain)\
**Post date:** [March 3, 2011, 5:02pm UTC](https://www.ruby-forum.com/t/how-to-stop-a-file-from-being-required/204018/4 "2011-03-03T17:02:42Z")

</div>

On Mar 3, 1:42pm, Steve K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> `foo\_test.rb  
> different. The solution:
> 
> require “File.expand\_path(test\_helper)”

the problem is… we do not know where the file is first getting  
loaded… so if we can unrequire a file that would be great. is that  
even possible though?

Many thanks for your help. they have been real helpful.

Kind regards,  
Usman H.

---

<div class="post-metadata">

**Author:** ![usmanhhussain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/5fc32e/32.png) [@usmanhhussain](https://www.ruby-forum.com/u/usmanhhussain)\
**Post date:** [March 3, 2011, 5:13pm UTC](https://www.ruby-forum.com/t/how-to-stop-a-file-from-being-required/204018/5 "2011-03-03T17:13:58Z")

</div>

On Thu, Mar 3, 2011 at 11:00 AM, Usman H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> the problem is… we do not know where the file is first getting  
> loaded… so if we can unrequire a file that would be great. is that  
> even possible though?

Here’s an article I wrote a couple years ago about tracking down  
accidental double-loads in Ruby apps:  
[http://avdi.org/devblog/2009/10/22/double-load-guards-in-ruby/](http://avdi.org/devblog/2009/10/22/double-load-guards-in-ruby/)

---

<div class="post-metadata">

**Author:** ![usmanhhussain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/5fc32e/32.png) [@usmanhhussain](https://www.ruby-forum.com/u/usmanhhussain)\
**Post date:** [March 3, 2011, 2:27pm UTC](https://www.ruby-forum.com/t/how-to-stop-a-file-from-being-required/204018/6 "2011-03-03T14:27:36Z")

</div>

On Thu, Mar 3, 2011 at 6:40 PM, Usman H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi Guys,
> 
> If i require a file by doing:  
> Dir[“features/whitelabel/step\_definitions/\*.rb”].each {|file| require  
> file }
> 
> is there something I can write before or after this to stop it from  
> loading if the file has already been loaded?

Does not require do that already? It loads the file only once unlike  
load.

> Also can I use an IF statement or something?

Seems to work:  
require ‘yaml’ if false #=\> nil  
require ‘yaml’ if true #=\> true
