ruby on rails - "require File.dirname(__FILE__)" -- how to safely undo filesystem dependency? -


Some Ruby libraries I'm using need it like this:

< Pre> requires File.dirname (__ FILE__) + '/specification_helper.rb' lib_dir = File.expand_path (File.join (File.dirname (__ FILE__), "lib")) File.join is required (File.dirname (__ FILE__), 'Lib / tools', 'version') File.expand_path (File.join (File.dirname (__ FILE__), 'datautils', 'conn')) < / Code>

Is this done in this way?

Is it possible to change this code (safe)? To remove this dependency on file system?

I actually use use in this plan the production code.

Relative files are required relative to current source space, which have several advantages:

  • The source tree can be transferred as complete and as long as we need it
  • Because we use the perfect path, we avoid accidental collision (loading another source with the same name in another library, or twice to reload the same source)
  • Modify Ruby's Search Path No code can be used

Should you use the modified Ruby Search Path, you can do it in a number of ways:

  1. Environment variable is playing with RUBYLIB
  • P> means solution 1 How to apply Ruby. To start the program, you will need a script like:

      @echo off rEM my_script.cmd set srcdir =% ~ dp0 \ .. \ path \ to \ source ruby ​​- I% Srcdir %% srcdir% \ my_script.rb  

    or:

      #! / Bin / sh srcdir = $ (cd $ (dirname $ 0) /. / Path / to / source & pwd) exec ruby- $ $ srcdir $ srcdir / my_script.rb  

    Solution 2 is practical, but not to avoid confrontation. You will usually do something like this:

      $ LOAD_PATH.unshift (File.expand_path (File.dirname (__ FILE__)))  

    The solution is unadvisable 3 , The less dependencies you have towards the environmental variable, then you'll be better.


  • Comments

    Popular posts from this blog

    c++ - Linux and clipboard -

    What is expire header and how to achive them in ASP.NET and PHP? -

    sql server - How can I determine which of my SQL 2005 statistics are unused? -