How to stop a file from being required

Discussion in 'Ruby' started by Usman Hussain, Mar 3, 2011.

  1. 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
     
    Usman Hussain, Mar 3, 2011
    #1
    1. Advertising

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

    On Thu, Mar 3, 2011 at 1:10 PM, Usman Hussain <>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.
     
    Adam Prescott, Mar 3, 2011
    #2
    1. Advertising

  3. On Thu, Mar 3, 2011 at 6:40 PM, Usman Hussain <> 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/
     
    Anurag Priyam, Mar 3, 2011
    #3
  4. [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)"
     
    Steve Klabnik, Mar 3, 2011
    #4
  5. On Mar 3, 1:42 pm, Steve Klabnik <> 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
     
    Usman Hussain, Mar 3, 2011
    #5
  6. Usman Hussain

    Avdi Grimm Guest

    On Thu, Mar 3, 2011 at 11:00 AM, Usman Hussain <> 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/

    --
    Avdi Grimm
    http://avdi.org
     
    Avdi Grimm, Mar 3, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Theule

    Stop Debugging doesn't stop in ASP.NET

    Matt Theule, Jul 23, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    774
    Matt Theule
    Jul 24, 2003
  2. Teemu Keiski
    Replies:
    3
    Views:
    2,110
    Jacob Yang [MSFT]
    Aug 18, 2003
  3. Son KwonNam
    Replies:
    11
    Views:
    2,646
    mr_organic
    Apr 9, 2004
  4. Will
    Replies:
    1
    Views:
    15,436
    Thomas Weidenfeller
    Nov 2, 2004
  5. Angus
    Replies:
    5
    Views:
    477
    Ben Bacarisse
    Jul 18, 2010
Loading...

Share This Page