“require File.dirname(__ FILE __)” - 如何安全地撤销文件系统依赖? [英] "require File.dirname(__FILE__)" -- how to safely undo filesystem dependency?

查看:208
本文介绍了“require File.dirname(__ FILE __)” - 如何安全地撤销文件系统依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的一些Ruby图书馆使用需要这样的语句:

  require File.dirname(__ FILE__)+'/specification_helper.rb'

lib_dir = File.expand_path(File.join(File.dirname(__ FILE__),lib))

需要File.join(File.dirname(__ FILE__),'lib / tools','version')

需要File.expand_path(File.join(File.dirname(__ FILE__) ,'datautils','conn'))

这种格式不会使您的代码不必要地依赖于文件系统的结构?



为什么作者这样做?



是否可能(安全地)改变这个代码以消除对文件系统的依赖性。

解决方案

我实际上使用生产代码中的这个方案。



需要相对于当前源位置的文件有几个优点:




  • 源树可以由于我们使用完整的路径,因此我们避免了意外的碰撞(在另一个库中加载同名的源码)或重新加载相同的来源两次)

  • 可以使用代码,无需修改ruby的搜索路径





如果您希望使用修改后的红宝石搜索路径,可以通过多种方式进行:


  1. 在ruby命令行上添加-I选项

  2. 修改使用环境变量RUBYLIB的源

  3. 中的$ LOAD_PATH变量

解决方案1意味着控制如何调用ruby。您需要一个脚本来启动程序,例如:

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

或者:

 #!/ bin / sh 
srcdir = $(cd $(dirname $ 0)/../ path / to / source&& pwd)
exec ruby​​ -I $ srcdir $ srcdir / my_script.rb

解决方案2可行,但不避免冲突。您通常会执行以下操作:

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

解决方案3是不合适的,对环境变量的依赖性越少,越好。


Some Ruby librararies I'm using use require statements like this:

require File.dirname(__FILE__) + '/specification_helper.rb'

lib_dir = File.expand_path(File.join(File.dirname(__FILE__), "lib"))

require File.join(File.dirname(__FILE__), 'lib/tools', 'version')

require File.expand_path(File.join(File.dirname(__FILE__), 'datautils', 'conn'))

Doesn't this format make your code needlessly dependent on the structure of the file system?

Why did the author do it this way?

Would it be possible to (safely) alter this code to remove this dependency on the filesystem?

解决方案

I actually use this scheme in production code.

Requiring files relative to the current source location has several advantages :

  • the source tree can be moved around as a whole and remains usable since we require sources relatively to each other.
  • since we use full paths, we avoid accidental collisions (loading a source with the same name in another library, or reloading the same source twice)
  • the code can be used without having to modify ruby's search path

Should you prefer to use a modified ruby search path, you can do it in multiple ways :

  1. adding -I options on the ruby command line
  2. modifying the $LOAD_PATH variable within the sources
  3. playing with the environment variable RUBYLIB

Solution 1 implies controlling how ruby is invoked. You'll need a script to start the program, such as:

@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 -I $srcdir $srcdir/my_script.rb

Solution 2 is workable but does not avoid collisions. You'll typically do something like :

$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))

Solution 3 is unadvisable, the less dependencies you'll have toward environment variables, the better you will be.

这篇关于“require File.dirname(__ FILE __)” - 如何安全地撤销文件系统依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆