Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to stop a file from being required

Reply
Thread Tools

How to stop a file from being required

 
 
Usman Hussain
Guest
Posts: n/a
 
      03-03-2011
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
 
Reply With Quote
 
 
 
 
Adam Prescott
Guest
Posts: n/a
 
      03-03-2011
[Note: parts of this message were removed to make it a legal post.]

On Thu, Mar 3, 2011 at 1:10 PM, Usman Hussain <(E-Mail Removed)>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...l.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.

 
Reply With Quote
 
 
 
 
Anurag Priyam
Guest
Posts: n/a
 
      03-03-2011
On Thu, Mar 3, 2011 at 6:40 PM, Usman Hussain <(E-Mail Removed)> 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

--
Anurag Priyam
http://about.me/yeban/

 
Reply With Quote
 
Steve Klabnik
Guest
Posts: n/a
 
      03-03-2011
[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)"

 
Reply With Quote
 
Usman Hussain
Guest
Posts: n/a
 
      03-03-2011
On Mar 3, 1:42*pm, Steve Klabnik <(E-Mail Removed)> wrote:
> [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
 
Reply With Quote
 
Avdi Grimm
Guest
Posts: n/a
 
      03-03-2011
On Thu, Mar 3, 2011 at 11:00 AM, Usman Hussain <(E-Mail Removed)> 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/d...uards-in-ruby/

--
Avdi Grimm
http://avdi.org

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to stop java service with command line parameter '-stop'? Will Java 1 11-02-2004 03:32 PM
How to stop a thread without using stop() Son KwonNam Java 11 04-09-2004 08:01 PM
Q. My browser doesn't stop, when I click on STOP. Jim Jones Computer Support 7 02-03-2004 09:47 PM
Stop Debugging doesn't stop in ASP.NET Matt Theule ASP .Net 7 07-24-2003 07:38 PM



Advertisments