How to stop a file from being required

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.

On Thu, Mar 3, 2011 at 1:10 PM, Usman H.
[email protected]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

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.

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)”

On Mar 3, 1:42pm, Steve K. [email protected] 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.

On Thu, Mar 3, 2011 at 11:00 AM, Usman H. [email protected]
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:

On Thu, Mar 3, 2011 at 6:40 PM, Usman H. [email protected]
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