Windows 上的 Ruby 目录中的 require 路径不起作用
我有一个小型 ruby 程序,需要
同一目录中的文件。程序在我的 Mac 上完美运行,当我在没有任何要求的情况下运行测试 ruby 脚本时,它也可以正常运行。默认情况下,ruby 程序似乎不会在当前目录中查找该文件。例如 .
目录。在 Windows 中,我需要在哪里更新它,以便 ruby 确实在当前目录中查找需求?
I've a small ruby program that require
files in the same directory. Program works perfect on my mac and when I run a test ruby script without any require it also works so. It seems the ruby program doesn't look in the current directory for the file by default. e.g. the .
dir. In windows where do I need to update this so ruby does look in the current dir for requires?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 Mac 可能正在运行 Ruby 1.8,而 Windows 可能正在运行 Ruby 1.9。从 1.9 开始,默认加载路径不再包含当前目录。常见的做法是将其添加到 ruby 文件顶部的 require 语句之前。
您还可以使用简写
$:
而不是$LOAD_PATH
:另一种选择是添加改为在命令行上加载路径:
Chances are that your Mac is running Ruby 1.8 and Windows is running Ruby 1.9. As of 1.9, the default load path no longer includes the current directory. A common practice is to add this to the top of your ruby file before your require statements
You can also use the shorthand
$:
instead of$LOAD_PATH
:Another alternative is adding the load path on the command line instead:
好吧,我现在明白了,从 1.9.2 开始,出于“安全”原因,他们不再允许 require 那样工作。我发现解决这个奇怪问题的最巧妙的方法是将“./”放在每个需求前面。
例如
Ok, I understand now since 1.9.2 for "Security" reasons they don't allow require to work like that anymore. The neatest way I found to solve it strangely was to put './' in front of every require.
e.g.
"."
已从$:
中删除,准确地说,已从 Ruby 1.9.2 中删除。 执行此操作如果您不相信我,请在 Ruby 1.8(Mac 上安装的内容)和 Ruby 1.9.2(Windows 计算机上安装的内容)上
。 为什么 Ruby 1.9.2 去掉“.”来自 LOAD_PATH,还有什么选择? 讨论了为什么“.”被删除。
"."
was removed from$:
was removed from Ruby 1.9.2 to be precise. Doon Ruby 1.8 (what's installed on your Mac) and Ruby 1.9.2 (what's installed on your windows machine) if you don't believe me.
Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative? discusses why "." was removed.