组合"捆绑执行"和"ruby -r"的正确方法?



让我们用一个随机的宝石来试验一下,哪个都没关系。

# Gemfile
gem "hashie"

让我们将我们的捆绑包安装在(半(非标准位置,这样ruby就找不到它:

bundle install --path=vendor/bundle
ruby -e 'require "hashie"'
# cannot load such file -- hashie (LoadError)

正如预期的那样,如果我们在上面添加bundle exec,那么ruby -e可以找到我们的宝石:

bundle exec ruby -e 'puts require "hashie"'
# true

但是,即使有bundle execruby -r也找不到我们的宝石!

bundle exec ruby -r hashie -e 'puts "win"'
# cannot load such file -- hashie (LoadError)

我发现将bundle execruby -r结合起来的唯一方法是:

bundle exec ruby -r 'bundler/setup' -r hashie -e 'puts "win"'
# win

捆绑执行的文档没有提到ruby -r,所以我在这里问,这是正确的方法吗?

-r 'bundler/setup'

是可以接受的。它的意思是"需要我的宝石文件中的所有宝石"。
"hashie"无法加载其依赖项,这就是发生"LoadError"的原因。

参考: bundler_setup

相关内容

最新更新