ruby on rails - "require File.dirname(__FILE__)" -- how to safely undo filesystem dependency? -
Some Ruby libraries I'm using need it
like this:
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:
- Environment variable is playing with RUBYLIB
@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
Post a Comment