How to stop a file from being required

U

Usman Hussain

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 Hussain
 
A

Adam Prescott

[Note: parts of this message were removed to make it a legal post.]

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.
 
A

Anurag Priyam

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
 
S

Steve Klabnik

[Note: parts of this message were removed to make it a legal post.]

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)"
 
U

Usman Hussain

[Note:  parts of this message were removed to make it a legal post.]

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

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 Hussain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top