正确组合`bundle exec`和`ruby -r`?

问题描述 投票:1回答:1

让我们尝试一个随机的宝石,这无关紧要。

# 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

documentation for bundle exec没有提及ruby -r,所以我在这里问,这是正确的方法吗?

ruby bundler
1个回答
0
投票
-r 'bundler/setup'

是可以接受的。这意味着“需要我的Gemfile中的所有宝石”。“ hashie”无法加载其依赖项,这就是发生“ LoadError”的原因。参考:bundler_setup

© www.soinside.com 2019 - 2024. All rights reserved.