简体   繁体   English

从Ruby脚本中检查gem路径

[英]Check gem path from within Ruby script

There are many questions and answers regarding checking the Ruby Gem path from the command line. 关于从命令行检查Ruby Gem路径有很多问题和答案。 However I haven't been able to work out how to check it from within a Ruby script. 但是我还无法弄清楚如何从Ruby脚本中检查它。 I need to do so because I have a require which works fine from irb, but fails within my script with a "cannot load such file" error. 我需要这样做是因为我有一个可以从irb正常工作的require ,但是在我的脚本中失败并出现“无法加载此类文件”错误。

How can I check where Ruby is searching for Gems from WITHIN the script? 我如何检查Ruby在脚本中从何处搜索Gems?

EDIT: 编辑:

The fix for my require turned out to be to add the required gem to the gemspec file as a dependency. require解决的方法是将所需的gem作为依赖项添加到gemspec文件中。 However that doesn't explain why irb was okay but the script wasn't...since the dependency was available, shouldn't they both have found it? 但是,这并不能解释为什么irb可以,但是脚本不可以...由于依赖关系可用,所以他们俩都找不到吗?

I think you're looking for $LOAD_PATH . 我认为您正在寻找$LOAD_PATH Being a list, you can append new search paths to it using push or unshift . 作为列表,您可以使用pushunshift为其添加新的搜索路径。 Also, if you're using a Gemfile for your script, you can try to execute your script using this command bundle exec ruby my_script.rb . 另外,如果您使用Gemfile作为脚本,则可以尝试使用此命令bundle exec ruby my_script.rb来执行脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM